【发布时间】:2016-11-22 11:38:42
【问题描述】:
我正在查看一些 c++ 代码并遇到一个我不明白的问题。
我有多个 cpp 文件,main.cpp 带有:
#include <iostream>
#include "classtest.h"
#include "test2.h"
using namespace std;
int main() {
foo test;
test.b=5;
...
查看包含,test2.h 为空,test2.cpp 仅包含以下内容:
struct foo{
int a;
};
包含的另一个标题,classtest.h 包含以下内容:
using namespace std;
class foo{
public:
int a;
int b;
};
如您所见,有一个同名的类和结构。我的问题是:为什么在我的 main 方法中键入 foo 是结构而不是类?头部test2.h中没有定义,main怎么访问?第二个问题:给定 foo 是结构(我正在使用 Eclipse 并看到它在其上定位鼠标光标),如何访问该结构不存在的字段 b?
抱歉,我是 C++ 的新手,我需要澄清这个疑问。谢谢。
【问题讨论】: