1

I have some trouble understanding the output from the following code:

int main(void)
{
    vector<TreeNode> tree;
    for(int i = 0; i < 50; i++)
    {
        TreeNode node;
        tree.push_back(node);
    }

    for(int i = 0; i < 50; i++)
    {
        Image image;
        image.Path = "TestPath";
        image.Votes = 0;

        TreeNode node = tree.at(i);
        node.images.push_back(image);
        node.images.push_back(image);
    }

    TreeNode node = tree.at(0);
    cout << "TEST" << endl;
    cout << node.images.size() << endl;
}

Output:

TEST
0

Why is the output 0 and what should I do to fix it? I would expect the size to be 2 and not 0. It has probably something to do with how structs work. My two defined structs is as follow:

typedef struct
{
    string Path;
    int Votes;
} Image;

typedef struct
{
    int treeIndex;
    float centroid;
    vector<float*> features;
    vector<Image> images;
} TreeNode;

Note that it is not an option for me to use classes instead of structs.

7
  • 2
    Note that structs and classes are the same except the default visibility (public/private).
    – Xeo
    Nov 11, 2011 at 14:21
  • 4
    The beauty of passing by copy vs by reference... by the way, your entire opening paragraph could be shortened to vector<TreeNode> tree(50);. Also, how is this marked C??
    – Kerrek SB
    Nov 11, 2011 at 14:21
  • 2
    why the typedef? that is obsolete old C style
    – PlasmaHH
    Nov 11, 2011 at 14:27
  • (And pushing back repeated copies of the same thing can be shortened to v.insert(v.end(), 2, image);.) Programmers hate repetition :-)
    – Kerrek SB
    Nov 11, 2011 at 14:27
  • 1
    Nothing is "returned". Stuff is "output". Functions return. Nov 11, 2011 at 14:29

6 Answers 6

9

vector::at returns a reference to an object.

Your line

TreeNode node = tree.at(i);

is creating a copy of the TreeNode in node, so when you add images to it, it's adding them to a new object that's not in your vector.

If you instead use

TreeNode& node = tree.at(i);

It will add the images correctly.

0
3

You are retrieving the node by value, and modify your local copy. Try

TreeNode &node = tree.at(i);
2

Your problem is in this line:

TreeNode node = tree.at(i);

Here you are making a copy of the node stored in the container. Your subsequent code modifies that copy, not the original. At the end, you access the original from the container, which is unmodified.

Instead, use

TreeNode& node = tree.at(i);

to make node refer to the object stored in the vector. The & means that node no is not a TreeNode object, but a reference to a TreeNode object. That is, every access to node will actually access the object it was bound to, i.e. the object returned by tree.at(i).

By the way, there are other things to note about your code.

First, you said you cannot use a class, but must use a struct. In C++, struct and class are exactly the same, except that structs have all members public by default. That is, the following two code examples are completely equivalent:

struct X
{
  int i;
  int k;
};

and

class X
{
public:
  int i;
  int k;
};

Indeed, you can even forward-declare your type with struct and then define it with class or vice versa.

The need to use struct instead of class can mean only one of two things: Either you need to include the file also in C (which does not knowe the class keyword), but in your case you couldn't do it anyway because C knows neither vector not string. Or you need to have your class a POD (plain old data) type, but again your structs are no POD tyoes because they have members of the non-POD types string and vector<T>. In other words, your structs are already classes, in every sense of the word.

In a related node, since there's no way you could include the definition of your structs in C, there's no point to use the typedef here. In C++,

struct Image
{
  string Path;
  int Votes;
};

already allows you to to refer to the type with Image (unlike in C where you would need to use struct Image to refer to the type in this case).

1
  • I need to use this with CUDA. CUDA supports both C and C++ but for me it only works when I use C syntax. Thats why :) Nov 11, 2011 at 14:46
1
TreeNode node = tree.at(i);

You are getting a copy of the node and modifying the copy, while the original stays unchanged.

1
TreeNode node = tree.at(i);
node.images.push_back(image);
node.images.push_back(image);

Here you've taken a copy of the TreeNode at index i, and added stuff to it. The copy is then destroyed immediately as it leaves scope.

Instead, take a reference so that you're working on the original object from the container:

TreeNode& node = tree.at(i);
node.images.push_back(image);
node.images.push_back(image);
0

Why are you first inserting the nodes and then modifying the nodes in the vector? You can insert the node once you have properly constructed (finished modifying) it. You dont need the first loop at all and Simply put the push at the end of second loop

1
  • This is just an easy example to describe my problem :) Nov 11, 2011 at 15:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.