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;
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
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 .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.