0

I am confused by this! I keep getting the error message that:

addSchool is not declared in the scope

and,

no matching function for call to school::addschool()

I can't see how these aren't declared. (I am also new to programming)

School is a class and one of the members of this class is a vector pointer.

.h File:

class school {
private:
    vector<school*> schools;

public:
    school();
    void addSchool(school *s);
};

.cpp file:

void school::addSchool(school *s){
    vector<school *> schools;
    s = new school();
    schools.push_back(s);
}

main function:

school *newSchool = new school();
school::addSchool(&newSchool);
delete newSchool;
Azeem
  • 11,148
  • 4
  • 27
  • 40
  • `newSchool ` is a pointer, so `&newSchool` is a pointer-to-pointer. – 001 Mar 15 '19 at 04:36
  • And none of this stands a chance of compiling; `addSchool` is neither a static class member nor takes a `school **` argument, both would break compilation with one single line from the call in `main`. Also, though not a compile-time error, the local `schools` object in `school::addSchool` hides the member variable `schools`, making that a flat logic bug. – WhozCraig Mar 15 '19 at 04:40
  • `newSchool` is a value that lives on the stack, and any attempt to store its address will result in `oldSchool` if used after that stack is torn down. – paddy Mar 15 '19 at 04:46
  • 1
    Why are you using "HEADER FILE" and "CPP file" and "MAIN FUNCTION" at the code level. Please just cut and paste your code. There seems to be some missing parts. – Martin York Mar 15 '19 at 04:46

2 Answers2

0

A few issues here:

  • Calling a non-static member function as if it were a static function. Change the function prototype to this:

    static void addSchool(school *s);

  • Passing a pointer to pointer to school in the function addSchool which takes only a pointer to school. Change the function call to this:

    school::addSchool(newSchool);

  • Declaring a local vector of schools in the addSchool function instead of using the already available private member vector<school *> schools. This will add only to the local vector of schools and not to the member vector of schools. Remove the following line in the addSchool function

    vector<school *> schools;

  • Allocating memory for school again in the addSchool function. This will cause a memory leak. You have to allocate memory only once. If you want to do it in main, remove the following line in the addSchool function.

    s = new school();

  • Deleting the memory allocated in main should be done only at the end of the program. Otherwise, you will be left hanging with a vector of pointers that are not valid.

I think that:

  • The vector of schools should also be a static because it is not specific to a particular instantiation. In that case, change the declaration of vector of schools to this:

    static vector<school *> schools;

See Demo

P.W
  • 26,289
  • 6
  • 39
  • 76
0

There seems to be 2 main problems here. The error you are getting is in how you are calling addSchool(). You are using the syntax to call a static function when you have it declared as a member. To call it as a member:

school.addSchool(newSchool);

Note using the . instead of ::, but also that newSchool is already a pointer so don't take the address of it. (As others already pointed out)

The other problems are run time errors in your addSchool method. You are creating a local vector, not using the member. You are also ignoring the parameter sent.

void school::addSchool(school *s) {
    for( i=0; i < s->size(); i++) {
        this.schools.push_back(s[i]);
    }
}

Now that I write that out, I realize there is also a bigger logical problem here in what this class actually is supposed to be. Its a school that contains a list of schools, so are you adding a school to that list or combining lists? While such things make sense for link lists and trees, your example says "schools" so I would rethink it. Perhaps your class should be a school district to contain a list of schools:

class District{
private:
    vector<school *> schools; // school declared elsewhere
public:
    District();
    inline int Size() { return this.schools.size(); };
    inline int Get(int i) { return this.schools[i]; };
    void addSchool(school *s);
    void addSchools(District *d);
}

// adding to the list
void District::addSchool(school *s) {
    this.schools.push_back(s);
}

// adding the contents of another like class, consider operator overloading + instead
void District::addSchools(District *d) {
    for( i=0; i < d->Size(); i++) {
        this.schools.push_back( d->Get(i) );
    }
}

Alternately, if you want a single static function as a master list, lookup the Singleton design pattern.

PuppyKhan
  • 115
  • 1
  • 6