Return vector of struct from function

I have a structure
1
2
3
4
struct MYSTRUCT{
    int index;
    char* name;
};


I have a function with this declaration
1
2
3
4
5
6
7
8
9
10
11
12
13
vector<MYSTRUCT> get_vector() {
    vector<MYSTRUCT> value;
    
    for (i=0; i<10; i++) {
        MYSTRUCT newvalue;
        
        newvalue.index = i;
        newvalue.name = "newvalue";
        value.push_back(newvalue);
        }

    return value;
}


What I want is to get a vector of MYSTRUCT values like this
 
vector<MYSTRUCT> newstruct = get_vector();


The get_vector() function is created inside a DLL file. I am trying to use it in my project, so I have to declare it into a .h file. The declaration is like this

 
vector<MYSTRUCT> get_vector();


At compiling I get an error for this too

header.h|25|error: expected constructor, destructor, or type conversion before '<' token|


I am a little lost because this is new for me. A little help will be so appreciated. Thank you!
Last edited on
expected constructor, destructor, or type conversion before '<' token


Did you forget to #include <vector> perhaps?
1
2
3
4
typedef struct {
    int index;
    char* name;
} MYSTRUCT;
What's with the old C style code? Why not use C++ style?
1
2
3
4
struct MyStruct{
    int index;
    char* name;
};
Better yet, why not use std::string instead of char*?
LB, the changes are done.

I included the <vector> header. Same error

Now I am completely lost.
Last edited on
What's the point of this anyway:
1
2
3
4
typedef struct {
    int index;
    char* name;
} MYSTRUCT;

Why still use that in C++? Isn't that the C equivalent of
1
2
3
4
struct MYSTRUCT {
    int index;
    char* name;
};

?
Don't worry about my struct declaration. It is modified now. The problem is that even if I include <vector> I get the same error

header.h|25|error: expected constructor, destructor, or type conversion before '<' token|
@alexbnc: Your compiler is set to compile C++ code, right? And not C? Because by the looks of those linkage errors, the compiler doesn't know what templates are, and doesn't know how to link them.

Errors wrote:
...with C linkage

...is a dead give away.

Wazzak
The compiler problem is solved now.
My project contains a GUI application and a Shared library.

The structure is defined into the .cpp and .h file of the Shared library. By including the .h file into the GUI project I can access the DLL functions and variables, those declared into it.

Everything is exported with no problem. The only problem is that the library header (that creates functions and variables references for the GUI projects) give an error when trying to compile the GUI project. It'l like the vector function is not understood. I can define a int, char*, or whatever function type but not vector<of STRUCT> function
Last edited on
Problem solved. I had to add using namespace std and the #include directive outside any header condition. Thanks to everybody
Topic archived. No new replies allowed.