954,202 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Issue creating a vector of a struct in C++

I'm trying to write a C++ program that uses some compound structs and vectors, and I'm getting a compile time error: multiple types in one declaration.

Here's the form my code's in:

#include <vector>
using namespace std;

struct Struct2 {
    int value1;
    int value2;
}; // end Struct2

struct Struct1 {
    int value;
    vector<Struct2> vec;
}; // end Struct1

class MyClass {
public:
    // constructor and various methods

private:
    vector<Struct1> classVec;
}; // end MyClass

That error points to the line where "classVec" is declared. I've tried some things like adding empty constructors to the structs, but that didn't change anything. Any pointers would be appreciated.

Thanks!

Bidromicles
Newbie Poster
9 posts since Sep 2005
Reputation Points: 10
Solved Threads: 0
 

works ok for me. your problem must be something else. what compiler? what are the errors?

Ancient Dragon
Retired & Loving It
Team Colleague
30,046 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,342
 

That's the only error I'm getting:

error: multiple types in one declaration

I just realized though that the error is on the ending bracket of "MyClass", not the declaration of "classVec". I'm using g++ to compile.

Bidromicles
Newbie Poster
9 posts since Sep 2005
Reputation Points: 10
Solved Threads: 0
 

Dang it, I'm an idiot. I didn't have a semicolon at the end of my second struct declaration. Yeah, that error makes sense to me now. Sorry for taking up your time, and thanks for the quick reply.

Bidromicles
Newbie Poster
9 posts since Sep 2005
Reputation Points: 10
Solved Threads: 0
 

Now if I can figure out how to close this thread, both of my problems will be solved...

Bidromicles
Newbie Poster
9 posts since Sep 2005
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You