1

Sorry, the title sounds a little confusing, let me explain.

I have a struct, which has a vector of structs, like this:

struct foo {
int x;
vector < foo > bar;
};

But I get an error of "No Instance of Overloaded Method" when I try this syntax:

foo a;
foo b;
b.x = 3;
a.bar.push_back(b);

Basically, I am just trying to create a "relationship" between a and b, with bar being the relationship vector containing all related instances of foo.

Any suggestions?

12
  • There's nothing wrong in the code. Which compiler do you use? Commented Nov 2, 2013 at 21:39
  • Same here, Code compile just fine with g++. Are you getting a run time error? Commented Nov 2, 2013 at 21:43
  • 1
    There is nothing with your code if it is C++. vector is not defined for C Commented Nov 2, 2013 at 21:44
  • @ryyker neither is overloading Commented Nov 2, 2013 at 21:44
  • 1
    @JoeZ: you cannot have vector< foo& >. Commented Nov 2, 2013 at 21:48

2 Answers 2

3

Your code is, unfortunately, illegal according to the C++ Standard, because you are passing an incomplete type as a template parameter to the Standard Library, which is prohibited in general and std::vector doesn't have an exception to the general rule.

The Boost documentation has a great explanation.

You can either use a container which explicitly supports incomplete types, as the Boost ones do, or use std::vector<std::unique_ptr<foo>>, because the Standard says in 20.9.1p5:

The template parameter T of unique_ptr may be an incomplete type.

Sign up to request clarification or add additional context in comments.

Comments

-1

May be it's your compiler, Mine gives no error or warnings.

#include <iostream> 
#include <vector>
using namespace std;



struct foo {
int x;
vector < foo > bar;
};


int main()
{
foo a;
foo b;
b.x = 3;
a.bar.push_back(b);
cout<<a.bar[0].x;
    cout<<"\n";
return 0;
} 

enter image description here

8 Comments

Nothing wrong except for violating rule 17.6.4.8 and causing undefined behavior.
Well, I didn't get any exception or warning whatsoever, so I guess we can live with this violation, can't we?
That's a very dangerous approach to undefined behavior.
Ok, that was me being casual? Here's my question, if there is a violation in documentation then shouldn't that reflect in compiler?
Obviously it is better when the compiler can detect undefined behavior and warn you about it, but the Standard doesn't require a diagnostic for this case -- and there are some well-known cases of undefined behavior which cannot be detected at compile time.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.