Problem with inheritance

?

----

Here are my files (problem follows the code):
--------------------------------------------------------------------------
// Group.h

#ifndef GROUP_H
#define GROUP_H

class Group {

public:
// default constructor
Group(string groupName = "BLANK");

void setName(string name);
string getName();

protected:
string name;

};

#endif
---------------------------------------------------------------------------
//Group.cpp

#include <string>
using std::string;

#include "Group.h"

// default constructor
Group::Group(string groupName)
:name(groupName)
{

}

void Group::setName(string name)
{
this->name = name;
}

string Group::getName()
{
return this->name;
}
--------------------------------------------------------------
//Area.h

#ifndef AREA_H
#define AREA_H

#include "Group.h"

class Area : public Group {

public:
Area(string areaName = "BLANK", int population = 0);

void setPopulation(int population);
int getPopulation();

protected:
int population;

};

#endif
----------------------------------------------------------------------------
----
//Area.cpp

#include "Area.h"

Area::Area(string areaName, int population)
: Group(areaName), population(population)
{

}

void Area::setPopulation(int population)
{
this->population = population;
}

int Area::getPopulation()
{
return this->population;
}
----------------------------------------------------------------------------
-----

OK, I'm working on a larger project, but I was having problems with
inheritance. This is a stripped down example that I still can;t get to work.
I'm compiling it with Visual C++ 6.0. When I try to compile the above files,
I get the following:

\group.h(10) : error C2629: unexpected 'class Group ('
\group.h(10) : error C2238: unexpected token(s) preceding ';'
\group.h(12) : error C2061: syntax error : identifier 'string'
\group.h(13) : error C2146: syntax error : missing ';' before identifier
'getName'
\group.h(13) : error C2501: 'string' : missing storage-class or type
specifiers
\group.h(16) : error C2146: syntax error : missing ';' before identifier
'name'
\group.h(16) : error C2501: 'string' : missing storage-class or type
specifiers
\group.h(16) : error C2501: 'name' : missing storage-class or type
specifiers
\area.h(11) : error C2629: unexpected 'class Area ('
\area.h(11) : error C2238: unexpected token(s) preceding ';'
\area.cpp(5) : error C2065: 'string' : undeclared identifier
\area.cpp(5) : error C2146: syntax error : missing ')' before identifier
'areaName'
\area.cpp(5) : error C2350: 'Area::Area::Area' is not a static member
\area.cpp(5) : error C2059: syntax error : ')'
\area.cpp(6) : error C2065: 'population' : undeclared identifier
\area.cpp(6) : error C2448: '<Unknown>' : function-style initializer appears
to be a function definition

I don't understand what the problem is! I'm fairly new to C++, but I tried
to follow an example in a book (with my own source files, though) and it
won't work. I might try typing the files in from the book, but
syntactically, I think everything is the same. Any help would be
appreciated!
 
J

John Carson

---- said:
Here are my files (problem follows the code):
--------------------------------------------------------------------------
// Group.h

#ifndef GROUP_H
#define GROUP_H

Here is your only problem. You have omitted:

#include <string>
using std::string;
 
D

Daniel T.

John Carson said:
Here is your only problem. You have omitted:

#include <string>
using std::string;

Don't ever put a using in an h file unless it is inside a scope. Better
to put std::string everywhere in the h file.
 
J

John Carson

Daniel T. said:
Don't ever put a using in an h file unless it is inside a scope.
Better to put std::string everywhere in the h file.


You are right. I was focussing on getting it to compile and ignoring design
issues.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,816
Latest member
nipsseyhussle

Latest Threads

Top