tags:

views:

1311

answers:

6

I'm trying to access a member structs variables, but I can't seem to get the syntax right. The two compile errors pr. access are: error C2274: 'function-style cast' : illegal as right side of '.' operator error C2228: left of '.otherdata' must have class/struct/union I have tried various changes, but none successful.

#include <iostream>

using std::cout;

class Foo{
public:
    struct Bar{
     int otherdata;
    };
    int somedata;
};

int main(){
    Foo foo;
    foo.Bar.otherdata = 5;

    cout << foo.Bar.otherdata;

    return 0;
}
+11  A: 

You only define a struct there, not allocate one. Try this:

class Foo{
public:
    struct Bar{
        int otherdata;
    } mybar;
    int somedata;
};

int main(){
    Foo foo;
    foo.mybar.otherdata = 5;

    cout << foo.mybar.otherdata;

    return 0;
}

If you want to reuse the struct in other classes, you can also define the struct outside:

struct Bar {
  int otherdata;
};

class Foo {
public:
    Bar mybar;
    int somedata;
}
schnaader
Thanks, completely forgot that. And works like a charm.
Psykocyber
The code is not precisely equivalent. In the first example, the name of the Bar struct is really Foo::Bar.
anon
You're right, Neil, edited my answer.
schnaader
+4  A: 

Bar is inner structure defined inside Foo. Creation of Foo object does not implicitly create the Bar's members. You need to explicitly create the object of Bar using Foo::Bar syntax.

Foo foo;
Foo::Bar fooBar;
fooBar.otherdata = 5;
cout << fooBar.otherdata;

Otherwise,

Create the Bar instance as member in Foo class.

class Foo{
public:
    struct Bar{
        int otherdata;
    };
    int somedata;
    Bar myBar;  //Now, Foo has Bar's instance as member

};

 Foo foo;
 foo.myBar.otherdata = 5;
aJ
I like this style better than the traditional C style "struct <Type> { } <Instance>".
Dave Van den Eynde
+4  A: 

You create a nested structure, but you never create any instances of it within the class. You need to say something like:

class Foo{
public:
    struct Bar{
        int otherdata;
    };
    Bar bar;
    int somedata;
};

You can then say:

foo.bar.otherdata = 5;
anon
+1  A: 

You are only declaring Foo::Bar but you don't instantiate it (not sure if that's the correct terminology)

See here for usage:

#include <iostream>

using namespace std;

class Foo
{
    public:
    struct Bar
    {
        int otherdata;
    };
    Bar bar;
    int somedata;
};

int main(){
    Foo::Bar bar;
    bar.otherdata = 6;
    cout << bar.otherdata << endl;

    Foo foo;
    //foo.Bar.otherdata = 5;
    foo.bar.otherdata = 5;

    //cout << foo.Bar.otherdata;
    cout << foo.bar.otherdata << endl;

    return 0;
}
stefanB
A: 
struct Bar{
        int otherdata;
    };

Here you have just defined a structure but not created any object of it. Hence when you say foo.Bar.otherdata = 5; it is compiler error. Create a object of struct Bar like Bar m_bar; and then use Foo.m_bar.otherdata = 5;

Naveen
A: 

This doesnt help at all my teacher has a something like this

class List {

private:

    // used to build a linked list of integers
  struct Node
  {
    Node *next;  // pointer to the next Node
    int data;    // the actual data in the node
  };

  Node *head_; // pointer to the head of the list
  Node *tail_; // pointer to the last node
  int size_;   // number of items on the list

  static int object_count_;       // number of Lists created
  Node *new_node(int data) const; // allocate node, initialize data/next

} how do i access that? when i have to call to new_node and make a function for it but i can classify it as Node *List::new_node(int data) const; when i cant access Node sense it is a private struct?

killa