- 论坛徽章:
- 0
|
不好意思要请教大家一个比较弱智的问题,希望大家不要介意,我google了好久也没找到答案。。。
在头文件t.h中定义了一个类
class MyClass
{
public:
MyClass ();
void p();
};
在t.cpp中的实现如下
#include "t.h"
#include <iostream>
MyClass::MyClass ()
{
}
void MyClass::p ( )
{
std::cout<<2<<std::endl;
return ;
}
在上面这种情况下,如果直接在t。cpp中写一个mian函数
如下
int main (int argc, char const* argv[])
{
MyClass m;
m.p();
return 0;
}
程序编译以及运行都没有问题
但是,如果我在t。h中加多这几条宏命令
#ifndef TT
#define TT
#else
//这里是类的定义,如上
#endif
则会有如下的错误
t.cpp|34| error: ‘MyClass’ has not been declared
t.cpp|34| error: ISO C++ forbids declaration of ‘MyClass’ with no type
t.cpp|41| error: ‘MyClass’ is not a class or namespace
t.cpp|49| error: ‘MyClass’ is not a class or namespace
|| /home/snake/devel/test/t.cpp: In function ‘int main(int, const char**)’:
t.cpp|58| error: expected `;' before ‘m’
t.cpp|58| warning: statement is a reference, not call, to function ‘MyClass’
t.cpp|58| warning: statement has no effect
t.cpp|59| error: ‘m’ was not declared in this scope
另外,如果main函数不写在t。cpp文件中,而是写在另一个文件mian。cpp中,如下
#include "t.h"
int main (int argc, char const* argv[])
{
MyClass m;
m.p();
return 0;
}
则不管t。h中是否有宏命令,都会有如下错误
main.cpp: In function ‘int main(int, const char**)’:
main.cpp:24: error: ‘MyClass’ was not declared in this scope
main.cpp:24: error: expected `;' before ‘m’
main.cpp:25: error: ‘m’ was not declared in this scope
t.cpp:34: error: ‘MyClass’ has not been declared
t.cpp:34: error: ISO C++ forbids declaration of ‘MyClass’ with no type
t.cpp:41: error: ‘MyClass’ is not a class or namespace
t.cpp:49: error: ‘MyClass’ is not a class or namespace
t.cpp: In function ‘int main(int, const char**)’:
t.cpp:58: error: expected `;' before ‘m’
t.cpp:59: error: ‘m’ was not declared in this scope
三个文件都在同一个目录中,请问为什么会出现这些错误,以及如何解决?
谢谢! |
|