0

I have problem with CDT GCC Builtin Compiler (not with the code).
Here is a small example that this issue is happen.
I have this code in 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;
}

At the last statement: int c = tal_vec.at(0).a;
Eclipse tell me: Field 'a' could not be resolved.

Already tell CDT GCC Builtin Compiler add this: -std=c++11 flag like here

In the other statement you can see that there is no error if i tell eclipse to go Tal tt2 = tal_vec.at(0); the after that to get filed a value.

can anyone suggest a solution?

Community
  • 1
  • 1
Tal
  • 1,145
  • 2
  • 14
  • 32

3 Answers3

0

I doubt that code you posted will even compile - it is because including tal's constructor, everything else is private in this class. You can't even create a single object of class tal according to above situation.

I run above code in ideone and following are compiler errors,

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
Pravar Jawalekar
  • 605
  • 1
  • 6
  • 18
  • Then its problem with eclipse that why it is flagging for a valid statement - given that you have made all members public in above code – Pravar Jawalekar Dec 30 '15 at 18:16
  • I know the problem on eclipse. I need a solution for eclipse CDT built in compiler. – Tal Dec 30 '15 at 18:17
0

Simply make it 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;
}

Worked fine in Code::BLocks 13.12

Rajnish
  • 419
  • 6
  • 21
0

in C++, interior of class is private by default :

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

Here, a, b and c, are "private", therefore you cannot access it outside the class.

However, you can access d from everywhere, just by telling variable.d = ...;

For e, only what inherits from T can use it.

This is true for variables, functions etc.

Pierre-Antoine Guillaume
  • 1,047
  • 1
  • 12
  • 28