Adarsh Jayakumar February 2016

Error when trying to use a structure as return type

I am a beginner in c++ . I am trying to create a function which can resize the current array of structures. Half the way im getting this error " 'myStruct' does not name a type" and " 'resizeArray' was not declared in this scope".

myStruct resizeArray(myStruct * f, int * sizeOfArray);
myStruct resizeArray(myStruct * f, int * sizeOfArray)
{
    *sizeOfArray *=2;
    //things to be added

}
struct myStruct{

    string Name;
    int days;
};
int main()
{

    struct myStruct f;
    int value = 0;
    int sizeOfArray =10;
    int number_of_friends =0;
    myStruct * ptr = new myStruct [sizeOfArray];
    while(value > 0)
    {
        cout << "Enter your friends Name " << endl;
        cin >> f.Name;
        cout << "How long you know him " << endl;
        cin >> f.days;
        cout << "Do you still wanna continue ? 0 for exit , any number to continue" << endl;
        cin >> value;
    }
    resizeArray(ptr,&sizeOfArray);
    if(sizeOfArray == number_of_friends)
    {

    }
    ptr[number_of_friends] = f;
    number_of_friends++;
    return 0;
}

Answers


John3136 February 2016

a) You try to use the struct before you define it

b) you say you are going to return a struct but then don't.

Move this:

struct myStruct{

    string Name;
    int days;
};

To before resizeArray(), then either make resizeArray() return an appropriate value or change it's signature.


Sam Varshavchik February 2016

Computers are logical things. They do things from start to finish, in order. They don't jump around, they move along in a straight line.

The very first thing the computer sees, when it reads your source file is:

myStruct resizeArray(myStruct * f, int * sizeOfArray);

The poor computer has no idea what you're talking about. It doesn't know anything about this mysterious entity called myStruct. It has no idea where it came from. It just started reading this file, with zero knowledge of what's in it.

So your poor computer loudly complains about it.

Please help your computer by declaring and defining your structures, before you use them. Your computer will thank you for it.


David van rijn February 2016

When you define your function, your structure is not yet defined, because that happens only a few lines later. So try to put your structure definition above your function definition. Or, better yet, put it in a header file.


Niobium_x February 2016

First of all, try defining structure above resize method. But it's not only one error.

Post Status

Asked in February 2016
Viewed 1,419 times
Voted 12
Answered 4 times

Search




Leave an answer