如何修复和编译此C 程序
How to fix and compile this C++ program
在nestclassdef.h中,我已经写了这样的代码
class A{
public:
class B{
public:
void BTest();
};
};
class B{
};
然后在nestclassdef.cpp中我正在编写像这样的代码
#include "nestClassDef.h"
#include<iostream>
void A::B::BTest(){
cout<<"Hello World!";
}
int main(){
A a;
A.B b;
b.BTest();
}
但是当我编译上述代码
时 g++ -o nestClassDef nestClassDef.cpp
我遇到这样的错误: -
nestClassDef.cpp: In member function ‘void A::B::BTest()’:
nestClassDef.cpp:5: error: ‘cout’ was not declared in this scope
nestClassDef.cpp: In function ‘int main()’:
nestClassDef.cpp:10: error: expected unqualified-id before ‘.’ token
nestClassDef.cpp:11: error: ‘b’ was not declared in this scope
我不知所措地解决此问题。值得庆幸的是,共享的任何理解。
对于cout错误:它在std
名称空间中,因此请使用std::cout
。
对于第二个错误:B
不是A's
成员,它是嵌套类型,因此您必须使用A::B b;
nestClassDef.cpp: In member function ‘void A::B::BTest()’:
nestClassDef.cpp:5: error: ‘cout’ was not declared in this scope
使用std::cout
代替cout
,或添加using namespace std;
(可能是在您的#include
语句之后)。
nestClassDef.cpp: In function ‘int main()’:
nestClassDef.cpp:10: error: expected unqualified-id before ‘.’ token
nestClassDef.cpp:11: error: ‘b’ was not declared in this scope
使用A::B
代替A.B
。
- 添加
using namespace std;
或使用std::cout
代替cout
- 使用
A::B
不是A.B
。点运算符用于对象或结构/工会。
相关文章:
- 这是使用回溯的 nqueen 问题,但我使用了动态 2d 数组,我的程序编译良好,但不返回任何输出
- C ++程序编译错误,找不到/访问文件
- C++程序编译没有问题,但无法运行
- Mongdb C++ 驱动程序编译错误 for document{}.
- 添加类型名会导致程序编译失败
- 将 C 程序更改为 C++ 程序 - 编译错误
- 程序编译,但当分解为函数时实际上不会移动电机
- 如何链接 glib-2.0 库进行 c/c++ 程序编译
- 为什么 gcc 和 clang 为我的程序编译为不同的"const"结果?
- 复数程序编译时的计算方法错误
- qt 命令行应用程序编译
- 程序编译和运行,但一段时间后,它停止使用Typedef关键字在CPP上工作
- QML应用程序编译一个 *form.ui.qml文件,但忽略了关联的.qml文件
- 为什么该程序编译但不显示任何输出
- 分割故障程序编译为共享库,但不静态
- 程序编译,但我认为开关被忽略
- 程序编译后崩溃
- 什么是程序编译中的PIC级别
- 尽管使用不存在的成员,但程序编译
- C++程序编译在 Ubuntu 中失败,但在 MacOSX 中工作