std vector at无法解决eclipse CDT GCC Builtin Compiler

时间:2015-12-30 18:01:38

标签: c++ eclipse c++11 eclipse-cdt

我遇到 CDT GCC内置编译器的问题(不带代码)。
以下是发生此问题的一个小例子 我在eclipse中有这个代码:

#include <vector>

typedef struct tal{
    tal()
    :a(0), b(0)
    {};
    int a;
    int b;
} Tal;

int main() {
    std::vector<Tal> tal_vec;
    Tal tt;
    tal_vec.push_back(tt);
    Tal tt2 = tal_vec.at(0);
    tt2.a;

    int c = tal_vec.at(0).a;
}

最后一句话:int c = tal_vec.at(0).a;
Eclipse告诉我:Field 'a' could not be resolved

已经告诉CDT GCC Builtin Compiler添加:-std=c++11标记为this

在另一个声明中,你可以看到,如果我告诉eclipse在{I}之后获得一个值,那么就没有错误。

有人可以提出解决方案吗?

3 个答案:

答案 0 :(得分:0)

我怀疑你发布的代码甚至会编译 - 这是因为包含tal's构造函数,其他所有内容都是private。根据上述情况,您甚至无法创建类tal的单个对象。

我在ideone中运行上面的代码,以下是编译器错误,

prog.cpp: In function 'int main()':
prog.cpp:4: error: 'tal::tal()' is private
prog.cpp:13: error: within this context
prog.cpp:7: error: 'int tal::a' is private
prog.cpp:16: error: within this context
prog.cpp:7: error: 'int tal::a' is private
prog.cpp:18: error: within this context

答案 1 :(得分:0)

只需将其设为public

#include <vector>

typedef class tal{
public:
    tal()
    :a(0), b(0)
    {};
    int a;
    int b;
} Tal;

int main() {
    std::vector<Tal> tal_vec;
    Tal tt;
    tal_vec.push_back(tt);
    Tal tt2 = tal_vec.at(0);
    tt2.a;

    int c = tal_vec.at(0).a;
}

Code :: BLocks 13.12

中工作得很好

答案 2 :(得分:0)

在C ++中,默认情况下类的内部是私有的:

class T
{
    int a;
    int b;
private:
    int c;
public:
    int d;
protected:
    int e;
}

这里,a,b和c是&#34;私有&#34;,因此你不能在课外访问它。

但是,你可以通过告诉variable.d = ...;

从任何地方访问d

对于e,只有从T继承的内容才能使用它。

对于变量,函数等都是如此。

相关问题