C++编译错误及解决办法
来源:互联网 发布:上海星米网络 编辑:程序博客网 时间:2024/05/22 17:21
编译错误一:XX does not name a type
编译错误二:field `XX' has incomplete type
编译错误一:XX does not name a type, 中文意思为“XX没有命名一个类型“
拿个例子来讲,如果两个类定义如下:
class B{
public:
B(){}
~B(){}
private:
A a;
};
class A{
public:
A(){}
~A(){}
private:
int a;
};
编译成则将报一个error:"A does not name a type"
报错的位置为红色那一行。
即使clase A和class B分别在两个文件定义,并且在定义B的文件头中#include了class A的头文件也同样会报这个错(这是因为编译和链接之间的先后关系造成的)。
解决该错误的办法:
在class B定义声明之前先声明一下class A, 如下:
class A;
class B{
public:
B(){}
~B(){}
private:
A a;
};
class A{
public:
A(){}
~A(){}
private:
int a;
};
参照: http://www.allegro.cc/forums/thread/586909
编译错误二:field `XX' has incomplete type
同样紧跟上面的例子,通过问题一的办法,第一个错误已经消失,但是马上第二个错误就出现了!还是同一个位置。
这个错误的意思,就是说class B中的XX域的类型不够完整,为什么呢?明明class A后面已经定义好了啊。其实原因还是和前面一样,在class B定义之前,我们也只是对class A进行了声明而并没有进行具体的定义,所以解决该错误的方法是:
将class B定义中的A域改用指针就行了。改正后代码为:
class A;
class B{
public:
B(){}
~B(){}
private:
A *a;
};
class A{
public:
A(){}
~A(){}
private:
int a;
};
这样,这段简单的代码才能没有错误的通过编译。
*********************************************************
[3]编译错误
You can get the error
cannot declare member function ‘static int Foo::bar()’ to have static linkage
if you declare a method to be static in your .cc file.
The reason is that static means something different inside .cc files than in class declarations It is really stupid, but the keyword static has three different meanings. In the .cc file, the static keyword means that the function isn't visible to any code outside of that particular file.
This means that you shouldn't use static in a .cc file to define one-per-class methods and variables. Fortunately, you don't need it. In C++, you are not allowed to have static variables or static methods with the same name(s) as instance variables or instance methods. Therefore if you declare a variable or method as static in the class declaration, you don't need the static keyword in the definition. The compiler still knows that the variable/method is part of the class and not the instance.
WRONG
Foo.h: class Foo { public: static int bar(); }; Foo.cc: static int Foo::bar() { // stuff }
WORKS
Foo.h: class Foo { public: static int bar(); }; Foo.cc: int Foo::bar() { // stuff }
A way to bypass this problem is to embed the defintion in the .h file, but this causes the function to be inline by default.
- C/C++ 常见编译错误及解决办法
- C++编译错误及解决办法
- VS2005编译错误集锦及解决办法
- 使用ffmpeg库编译错误及解决办法
- 使用ffmpeg库编译错误及解决办法
- 编译ncurses错误处理及解决办法
- Altium Designer编译常见错误及解决办法
- 编译ncurses错误处理及解决办法
- IAR编译cstack错误及解决办法
- 编译ncurses错误处理及解决办法
- 编译ncurses错误处理及解决办法
- linux下编译qt4错误及解决办法
- 编译安卓2.3错误遇见的错误及解决办法
- drcom.c编译错误及解决方法
- C 编译错误 及解决方法总结
- 常见VC++编译错误及解决办法[VC用户必看]
- MFC 编译连接时产生错误及解决办法
- MFC 编译连接时产生错误及解决办法
- 求1+2+3+···
- 安卓表格布局android:collapseColumns,android:shrinkColumns和stretchColumn
- android 鸟姐的开发之路---开发环境搭建(最新版)
- 系统常用操作
- 快速替换字母、数字为Times New Roman字体
- C++编译错误及解决办法
- NeHe教程Qt实现——lesson13
- 浅谈SQLite-SQLite不支持的SQL语法总结
- 好网站推荐 [不断更新]
- 【Android Training - Connectivity】优化下载的效率[Lesson 0 - 章节概览]
- C#如何读取excel数据
- 1692. Cover
- 中国石油大学(华东)CSDN俱乐部成立仪式
- POJ 1809 Regetni 奇偶性