c++ - 如何从嵌套在命名空间中的类中调用成员函数?
问题描述
我想从不同的文件调用嵌套在命名空间中的类成员函数,但我不知道如何。
例如:
如何调用someFunc()
位于 code.h 中并嵌套在 main.cpp 命名空间“程序”中的类成员函数。
//code.h
#include <iostream>
namespace program
class test {
private:
int x;
public:
test()
{
test::x = 10;
};
someFunc()
{
cout << x << " ";
};
};
解决方案
你的代码有一些问题
#include <iostream>
namespace program { // <-- braces missing
class test
{
private:
int x;
public:
test()
{
test::x = 10; // <-- test:: is unnecessary but no error
};
void someFunc() // <-- return type missing
{
std::cout << x << " "; // <-- namespace std missing
};
};
} // <-- braces missing
int main() {
program::test t;
t.someFunc();
}
推荐阅读
- r - 如何在r中更改图例的字体
- android - Android Studio 因英特尔芯片内部错误而关闭
- java - Google 帐单库 - 在多台设备上访问订阅
- node.js - Node.js 日志格式化程序:系统时区更改后更新日期时间
- javascript - 用选定的值集反应多个复选框
- csv - 如何将地图插入到未知列的表中?
- database - YugabyteDB 中的排序规则支持?
- c# - 我可以减少对构造函数的依赖注入吗
- python - 使用 pyspark 数据框验证多个电子邮件地址列并将两列值连接到由管道分隔的 1 列值中
- javascript - 用JS在地址簿中显示搜索框