Reputation: 1573
This may or may not be an IDE-specific question.
So I made a project using Code::Blocks. In the project folder there's Main.cpp, Person.h, and Person.cpp:
// main.cpp
#include "Person.h"
int main()
{
int x = 1, y = 2, z = 3;
Person p = new Person(x, y, z);
}
I'm getting an error saying that Person and "p" were not declared in this scope. But I declared them in Person.h. I included the Person.h header file. So, the code that is there should be in Main, too, right?
Here are the contents of Person.h:
#ifndef PERSON_H
#define PERSON_H
class Person
{
public:
Person();
Person(int, int, int);
private:
int x, y, z;
};
#endif
Upvotes: 0
Views: 89
Reputation: 5369
Write the constructor inside the class.
Person(int a, int b, int c)
{
x=a;
y=b;
z=c;
}
And change
Person p = new Person(x, y, z);
to
Person p(x, y, z); //if you just want to make an object p
or to
Person* p = new Person(x, y, z); //if you want a pointer p to point to a Person object
//your code goes here
//But in this case, you will have to explicitly deallocate the space you've allocated for p
delete p;
Upvotes: 3