C++ Inheritance Issues - c++

I am having some issues regarding inheritance. I have a class of Person, and a class of Student:Person, Employee:Person. The errors that I am getting baffle me – I do not understand why I am getting them. I used tiny paste to paste the code as I thought it would take too much space up here. If I should post question elsewhere, let me know. Thanks.
Code Files:
main.cpp – http://tinypaste.com/0775bea3
Person.h – http://tinypaste.com/657638ef
Person.cpp – http://tinypaste.com/934ee106
Student.h – http://tinypaste.com/260fa4bb
Student.cpp – http://tinypaste.com/b6259aa4
Employee.h – http://tinypaste.com/f8b53d36
Employee.cpp – http://tinypaste.com/1d939927
Here are the errors that I am getting:
1>------ Build started: Project: PR4_Students, Configuration: Debug Win32 ------
1>Build started 2/18/2012 11:14:27 PM.
1>InitializeBuildStatus:
1> Touching "Debug\PR4_Students.unsuccessfulbuild".
1>ClCompile:
1> main.cpp
1>\\cs1\cs_students\mlindahl15\cs273\pr4_students\student.h(15): error C2969: syntax error : ';' : expected member function definition to end with '}'
1>\\cs1\cs_students\mlindahl15\cs273\pr4_students\student.h(16): error C2059: syntax error : 'inline function header'
1>\\cs1\cs_students\mlindahl15\cs273\pr4_students\student.h(16): error C2630: ';' found in what should be a comma-separated list
1>\\cs1\cs_students\mlindahl15\cs273\pr4_students\employee.h(15): error C2969: syntax error : ';' : expected member function definition to end with '}'
1>\\cs1\cs_students\mlindahl15\cs273\pr4_students\employee.h(16): error C2059: syntax error : 'inline function header'
1>\\cs1\cs_students\mlindahl15\cs273\pr4_students\employee.h(16): error C2630: ';' found in what should be a comma-separated list
1> Student.cpp
1>\\cs1\cs_students\mlindahl15\cs273\pr4_students\student.h(15): error C2969: syntax error : ';' : expected member function definition to end with '}'
1>\\cs1\cs_students\mlindahl15\cs273\pr4_students\student.h(16): error C2059: syntax error : 'inline function header'
1>\\cs1\cs_students\mlindahl15\cs273\pr4_students\student.h(16): error C2630: ';' found in what should be a comma-separated list
1>\\cs1\cs_students\mlindahl15\cs273\pr4_students\student.cpp(8): error C2084: function 'Student::Student(void)' already has a body
1> \\cs1\cs_students\mlindahl15\cs273\pr4_students\student.h(15) : see previous definition of '{ctor}'
1> Employee.cpp
1>\\cs1\cs_students\mlindahl15\cs273\pr4_students\employee.h(15): error C2969: syntax error : ';' : expected member function definition to end with '}'
1>\\cs1\cs_students\mlindahl15\cs273\pr4_students\employee.h(16): error C2059: syntax error : 'inline function header'
1>\\cs1\cs_students\mlindahl15\cs273\pr4_students\employee.h(16): error C2630: ';' found in what should be a comma-separated list
1>\\cs1\cs_students\mlindahl15\cs273\pr4_students\employee.cpp(8): error C2084: function 'Employee::Employee(void)' already has a body
1> \\cs1\cs_students\mlindahl15\cs273\pr4_students\employee.h(15) : see previous definition of '{ctor}'
1> Generating Code...
1>
1>Build FAILED.
1>
1>Time Elapsed 00:00:05.64
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

I didn't analyze the whole code, but you seem to be confused how to declare calls to the base class constructor;
class Student : public Person
{
...
Student() : Person();
...
};
The call to the base class constructor should be done on the actual implementation of the constructor only. Since you already do that with
Student::Student() : Person() {
you can just change the declaration to
class Student : public Person
{
...
Student();
...
};
and things should turn out better.
Edit: Adding the answer to a follow-up question below;
The line
Employee(string department, string jobTitle, int yearOfHire)
: Person(name, socialSecurityNumber, age, gender, address, phoneNumber) {
does for the same reason not really make sense. If you want to be able to construct an Employee with all those parameters, you need to instead declare the constructor as;
Employee(string department, string jobTitle, int yearOfHire, name,
socialSecurityNumber, age, gender, address, phoneNumber) {
and implement it as
Employee::Employee(string department, string jobTitle, int yearOfHire, name,
socialSecurityNumber, age, gender, address, phoneNumber)
: Person(name, socialSecurityNumber, age, gender, address, phoneNumber) {
thus passing on the parameters to the base class constructor.

At line 15 of Students.h:
Student() : Person();
That's invalid. Either you need to completely define the constructor there, or not at all.
So:
Student() : Person() { some code; };
or:
Student();
and put the actual code in your implementation file.

Related

Using member function of Class in a Vector [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
EDIT: You need to make a vector like this:
vector myVector(5);
not like this:
vector myVector[5];
Instead of vector<team> players[5]; vector<team> players(5); has to be. With this operation you'll create a vector of 5 players. In your code 5 empty vectors are created.
You should get a lot more errors, actually. I get the following:
cl /nologo /EHsc /Za /W4 stackoverflow.cpp
stackoverflow.cpp
stackoverflow.cpp(5) : error C2146: syntax error : missing ';' before identifier 'name'
stackoverflow.cpp(5) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
stackoverflow.cpp(9) : error C2061: syntax error : identifier 'string'
stackoverflow.cpp(10) : error C2146: syntax error : missing ';' before identifier 'getName'
stackoverflow.cpp(10) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
stackoverflow.cpp(10) : warning C4183: 'getName': missing return type; assumed to be a member function returning 'int'
stackoverflow.cpp(22) : error C2511: 'void team::setName(std::string)' : overloaded member function not found in 'team'
stackoverflow.cpp(3) : see declaration of 'team'
stackoverflow.cpp(23) : error C2065: 'name' : undeclared identifier
stackoverflow.cpp(23) : error C2065: 'b' : undeclared identifier
stackoverflow.cpp(30) : error C2556: 'std::string team::getName(void)' : overloaded function differs only by return type from 'int team::getName(void
stackoverflow.cpp(10) : see declaration of 'team::getName'
stackoverflow.cpp(30) : error C2371: 'team::getName' : redefinition; different basic types
stackoverflow.cpp(10) : see declaration of 'team::getName'
stackoverflow.cpp(31) : error C2065: 'name' : undeclared identifier
stackoverflow.cpp(42) : error C2039: 'setRuns' : is not a member of 'std::vector<team,std::allocator<_Ty>>'
with
[
_Ty=team
]
stackoverflow.cpp(43) : error C2039: 'setName' : is not a member of 'std::vector<team,std::allocator<_Ty>>'
with
[
_Ty=team
]
stackoverflow.cpp(45) : error C2039: 'getName' : is not a member of 'std::vector<team,std::allocator<_Ty>>'
with
[
_Ty=team
]
stackoverflow.cpp(45) : error C2039: 'getRuns' : is not a member of 'std::vector<team,std::allocator<_Ty>>'
with
[
_Ty=team
]
Class names of the C++ standard library are prefixed with std::. That's just part of their name. It is good practice to just always use the full name. In particular, it is very bad practice to use using namespace std; at global scope in a header file.
So let's remove using namespace std and write std:: everywhere.
team.h:
#include<string>
class team {
std::string name;
int runs;
public:
void setName(std::string a);
std::string getName();
void setRuns(int b);
int getRuns();
};
team.cpp:
#include<string>
#include "team.h"
void team::setRuns(int b) {
runs=b;
}
void team::setName(std::string a) {
name=b;
}
int team::getRuns() {
return runs;
}
std::string team::getName() {
return name;
}
main.cpp:
#include<iostream>
#include<vector>
#include<cstdio>
#include "team.h"
int main() {
std::vector<team> players[5];
players[0].setRuns(10);
players[0].setName("Michael");
printf("%s %d",players[0].getName(),players[0].getRuns());
system("pause");
return 0;
}
This removes most errors:
stackoverflow.cpp(22) : error C2065: 'b' : undeclared identifier
stackoverflow.cpp(39) : error C2039: 'setRuns' : is not a member of 'std::vector<team,std::allocator<_Ty>>'
with
[
_Ty=team
]
stackoverflow.cpp(40) : error C2039: 'setName' : is not a member of 'std::vector<team,std::allocator<_Ty>>'
with
[
_Ty=team
]
stackoverflow.cpp(42) : error C2039: 'getName' : is not a member of 'std::vector<team,std::allocator<_Ty>>'
with
[
_Ty=team
]
stackoverflow.cpp(42) : error C2039: 'getRuns' : is not a member of 'std::vector<team,std::allocator<_Ty>>'
with
[
_Ty=team
]
The a in setRuns is certainly a typo. We'll also fix that. I'll also just remove the unnecessary system("pause");. Now we have code which exhibits only the error you asked about.
Let's take a closer look at the following line:
std::vector<team> players[5]
I think the misunderstanding here is that [5] specifies the size of the std::vector. This is a misconception. A std::vector has no fixed size and will start at 0 elements by default. It does not need any [] syntax for initialization. The [] syntax here denotes an array. An array is a fixed-size collection of elements.
So what you created here is an array of 5 vectors. Apparently, that's not at all what you wanted. Just replace the [5] with (5) to get the meaning of "vector that starts with 5 elements":
std::vector<team> players(5);
Now it compiles. But it will probably crash at run-time, because you also use printf incorrectly:
printf("%s %d",players[0].getName(),players[0].getRuns());
printf is a C function which was designed long before C++ existed. %s means that a C-style string is expected. You could provide one like this:
printf("%s %d",players[0].getName().c_str(),players[0].getRuns());
Or you just use C++ streams:
std::cout << players[0].getName() << " " << players[0].getRuns();

Initializing object of a class inside another class

I have a class MySeqBuildBlockModule that I am inheriting from: public SeqBuildBlock.
Other than constructor and destructor, this class MySeqBuildBlockModule has a method: prep.
class MySeqBuildBlockModule: public SeqBuildBlock
{
friend class SeqBuildBlockIRns;
public:
MySeqBuildBlockModule (SBBList* pSBBList0, long TI1_In, long TI2_In) // more arguements in this constructor of derived class
: SeqBuildBlock (pSBBList0)
{
TI1 = TI1_In; //in us
TI2 = TI2_In; //in us
pSBBList2 = pSBBList0;
//SeqBuildBlockIRns myIRns_3(pSBBList2); //Defined in libSBB.h
}
//~MySeqBuildBlockModule(){}
virtual bool prep (MrProt* pMrProt, SeqLim* pSeqLim, SeqExpo* pSeqExpo);
virtual bool run (MrProt* pMrProt, SeqLim* pSeqLim, SeqExpo* pSeqExpo, sSLICE_POS* pSLC);
protected:
private:
long TI1; //in us
long TI2; //in us
SBBList* pSBBList2;
SeqBuildBlockIRns myIRns_3(pSBBList2); // Line 106
};
bool MySeqBuildBlockModule::prep(MrProt* pMrProt, SeqLim* pSeqLim, SeqExpo* pSeqExpo)
{
NLS_STATUS lStatus;
double dEnergyAllSBBs_DK = myIRns_3.getEnergyPerRequest(); //Line 113
// Now we prepare:
lStatus = pSBBList->prepSBBAll(pMrProt, pSeqLim, pSeqExpo, &dEnergyAllSBBs_DK); // Line 116
if (lStatus ) {
cout << "DK: An error has occurred while preparing SBBs"<< endl;
}
return lStatus;
}
I would have like to intiantiate an object "myIRns_3" of a class defined in third party library
SeqBuildBlockIRns myIRns_3(pSBBList2);
and would like to access it from the prep function as:
double dEnergyAllSBBs_DK = myIRns_3.getEnergyPerRequest();
I tried to instantiate following in either private section or in constructor; but w/o any success:
SeqBuildBlockIRns myIRns_3(pSBBList2);
ERRORS encountered:
When I tried to do it inside the constructor, I get the following errors:
MySBBModule.h(113) : error C2065: 'myIRns_3' : undeclared identifier
MySBBModule.h(113) : error C2228: left of '.getEnergyPerRequest' must have class/struct/union type
MySBBModule.h(116) : error C2065: 'pSBBList' : undeclared identifier
MySBBModule.h(116) : error C2227: left of '->prepSBBAll' must point to class/struct/union
When I tried to do it in private section, I get the following errors:
MySBBModule.h(106) : error C2061: syntax error : identifier 'pSBBList2'
MySBBModule.h(113) : error C2228: left of '.getEnergyPerRequest' must have class/struct/union type
MySBBModule.h(116) : error C2065: 'pSBBList' : undeclared identifier
MySBBModule.h(116) : error C2227: left of '->prepSBBAll' must point to class/struct/union
PS: I have marked the line no in the code that I am posting.
Any help would be appreciated.
Regards,
DK
Your problem is that the below is declaring a function:
SeqBuildBlockIRns myIRns_3(pSBBList2);
What you want to do is declare a member:
SeqBuildBlockIRns myIRns_3;
And then in your constructor construct it with the argument pSBBList2:
MySeqBuildBlockModule (SBBList* pSBBList0, long TI1_In, long TI2_In) // more arguements in this constructor of derived class
: SeqBuildBlock (pSBBList0),
myIRns_3(pSBBList2) // <- construct
{ }

new to CPP: Compiler error

Compilation fails with the following output.
Any thoughts please..
PStore.cpp
PStore.cpp(169) : error C2220: warning treated as error - no 'object' file generated
PStore.cpp(169) : warning C4091: '' : ignored on left of 'bool' when no variable is declared
PStore.cpp(169) : error C2143: syntax error : missing ';' before 'inline function header'
PStore.cpp(170) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
PStore.cpp(170) : error C2556: 'int PStore::getVersion(std::string &)' : overloaded function differs only by return type from 'bool PStore::getVersion(std::string &)'
../include\PStore.h(48) : see declaration of 'PStore::getVersion'
PStore.cpp(170) : error C2371: 'PStore::getVersion' : redefinition; different basic types
../include\PStore.h(48) : see declaration of 'PStore::getVersion'
Here is the code snippet:
bool PStore::getVersion(std::string& version)
{
AMPI_INFO("[API]");
return getVersionNoLogging(version);
}
bool PStore::getVersionNoLogging(std::string& version)
{
version = AMPI_PStore_VERSION " " __DATE__ " " __TIME__;
return true;
}
Please post your code so that all errors can be explained.
One of the errors is obvious however: you can't have two functions with the same parameters and the same name.
In your case you have int PStore::getVersion(std::string &) and bool PStore::getVersion(std::string &), which is not legal.
Either change the name of one of the functions, or change the type or number of parameters of one of the functions.

Templated function with two type parameters fails compile when used with an error-checking macro

Because someone in our group hates exceptions (let's not discuss that here), we tend to use error-checking macros in our C++ projects. I have encountered an odd compilation failure when using a templated function with two type parameters. There are a few errors (below), but I think the root cause is a warning:
warning C4002: too many actual parameters for macro 'BOOL_CHECK_BOOL_RETURN'
Probably best explained in code:
#include "stdafx.h"
template<class A, class B>
bool DoubleTemplated(B & value)
{
return true;
}
template<class A>
bool SingleTemplated(A & value)
{
return true;
}
bool NotTemplated(bool & value)
{
return true;
}
#define BOOL_CHECK_BOOL_RETURN(expr) \
do \
{ \
bool __b = (expr); \
if (!__b) \
{ \
return false; \
} \
} while (false) \
bool call()
{
bool thing = true;
// BOOL_CHECK_BOOL_RETURN(DoubleTemplated<int, bool>(thing));
// Above line doesn't compile.
BOOL_CHECK_BOOL_RETURN((DoubleTemplated<int, bool>(thing)));
// Above line compiles just fine.
bool temp = DoubleTemplated<int, bool>(thing);
// Above line compiles just fine.
BOOL_CHECK_BOOL_RETURN(SingleTemplated<bool>(thing));
BOOL_CHECK_BOOL_RETURN(NotTemplated(thing));
return true;
}
int _tmain(int argc, _TCHAR* argv[])
{
call();
return 0;
}
Here are the errors, when the offending line is not commented out:
1>------ Build started: Project: test, Configuration: Debug Win32 ------
1>Compiling...
1>test.cpp
1>c:\junk\temp\test\test\test.cpp(38) : warning C4002: too many actual parameters for macro 'BOOL_CHECK_BOOL_RETURN'
1>c:\junk\temp\test\test\test.cpp(38) : error C2143: syntax error : missing ',' before ')'
1>c:\junk\temp\test\test\test.cpp(38) : error C2143: syntax error : missing ';' before '{'
1>c:\junk\temp\test\test\test.cpp(41) : error C2143: syntax error : missing ';' before '{'
1>c:\junk\temp\test\test\test.cpp(48) : error C2143: syntax error : missing ';' before '{'
1>c:\junk\temp\test\test\test.cpp(49) : error C2143: syntax error : missing ';' before '{'
1>c:\junk\temp\test\test\test.cpp(52) : error C2143: syntax error : missing ';' before '}'
1>c:\junk\temp\test\test\test.cpp(54) : error C2065: 'argv' : undeclared identifier
1>c:\junk\temp\test\test\test.cpp(54) : error C2059: syntax error : ']'
1>c:\junk\temp\test\test\test.cpp(55) : error C2143: syntax error : missing ';' before '{'
1>c:\junk\temp\test\test\test.cpp(58) : error C2143: syntax error : missing ';' before '}'
1>c:\junk\temp\test\test\test.cpp(60) : error C2143: syntax error : missing ';' before '}'
1>c:\junk\temp\test\test\test.cpp(60) : fatal error C1004: unexpected end-of-file found
1>Build log was saved at "file://c:\junk\temp\test\test\Debug\BuildLog.htm"
1>test - 12 error(s), 1 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Any ideas? Thanks!
The preprocessor has no understanding of C++! It simply performs lexical substitutions.
When you declare a multi-argument macro, the arguments are separated by a comma. Since you have a comma in your macro invocation, you're calling the macro with multiple parameters, despite it being declared to only take one argument.
Parentheses are understood by the PP as forming a token group, so everything inside a set of parentheses is one big token.
Macros are unaware of the language, and work only with lexical tokens. A comma separates arguemnts for a macro, thus the following code attemps to 'invoke' the macro with two arguments:
BOOL_CHECK_BOOL_RETURN(DoubleTemplated<int, bool>(thing));
DoubleTemplated<int and bool>(thing). That's the warning you are seeing, and cause of the other errors as well. The following is the correct way to protect against , in template arguments list:
BOOL_CHECK_BOOL_RETURN((DoubleTemplated<int, bool>(thing)));
In the line that doesn't compile, that comma is interpreted by the preprocessor as a delimiter of the macro arguments.
In the C99 standard (I haven't got the C++ standard to hand, but it will be very similar), we see the following in section 6.10.3:
The sequence of preprocessing tokens bounded by the outside-most
matching parentheses forms the list of arguments for the function-like
macro. The individual arguments within the list are separated by comma
preprocessing tokens, but comma preprocessing tokens between matching
inner parentheses do not separate arguments.
So that's why your second macro instantiation works.

Why does the order of my #includes matter? (C++)

I've created a header file called "list_dec.h", put it in a folder "C:\Headers", and set my compiler to include files from "C:\Headers", so now I can do things like
#include<list_dec.h>
int main(){return(0);}
but when I try to do something like
#include<iostream>
#include<list_dec.h>
int main(){return(0);}
I get an error (not anything specific, just a huge list of syntax errors in "list_dec.h", which I know aren't real because I've been able to compile it as both a main.cpp file and a .h file in a separate project). However, when I change to order so "list_dec.h" is on top:
#include<list_dec.h>
#include<iostream>
int main(){return(0);}
all of the errors go away. So why does the order of the error matter?
NB: As far as I know, this occurs when I use "list_dec.h" with all header files, but the files I'm absolutely positive it occurs in are:
#include<iostream>
#include<vector>
#include<time.h>
#include<stdlib.h>
EDIT: These are the errors I get when "list_dec.h" is below any other header:
c:\headers\list_dec.h(14) : error C2143: syntax error : missing ')' before 'constant'
c:\headers\list_dec.h(51) : see reference to class template instantiation 'list<T,limit>' being compiled
c:\headers\list_dec.h(14) : error C2143: syntax error : missing ';' before 'constant'
c:\headers\list_dec.h(14) : error C2059: syntax error : ')'
c:\headers\list_dec.h(14) : error C2238: unexpected token(s) preceding ';'
c:\headers\list_dec.h(69) : warning C4346: 'list<T,limit>::{ctor}' : dependent name is not a type
prefix with 'typename' to indicate a type
c:\headers\list_dec.h(69) : error C2143: syntax error : missing ')' before 'constant'
c:\headers\list_dec.h(69) : error C2143: syntax error : missing ';' before 'constant'
c:\headers\list_dec.h(69) : error C2988: unrecognizable template declaration/definition
c:\headers\list_dec.h(69) : error C2059: syntax error : 'constant'
c:\headers\list_dec.h(69) : error C2059: syntax error : ')'
c:\headers\list_dec.h(78) : error C2065: 'T' : undeclared identifier
c:\headers\list_dec.h(78) : error C2065: 'limit' : undeclared identifier
c:\headers\list_dec.h(78) : error C2065: 'T' : undeclared identifier
c:\headers\list_dec.h(78) : error C2065: 'limit' : undeclared identifier
c:\headers\list_dec.h(79) : error C2143: syntax error : missing ';' before '{'
c:\headers\list_dec.h(79) : error C2447: '{' : missing function header (old-style formal list?)
If it helps, these are the lines mentioned in the errors (14, 69, 78, and 79):
Line 14: list(const T& NULL); (A constructor for "list" class)
Line 69: inline list<T, limit>::list(const T& NULL): (Definition for the constructor, also, the colon at the end is intentional, It part of the definion ie: void x(int n): VAR(n).)
Line 78: inline list<T, limit>::list(const list<T, limit>& lst) (def for the copy constructor)
Line 79: { (the begining of the list-copy contructor)
And a lot of people want to see the beginning of "list_dec.h":
template<class T, size_t limit>
class list
NB: These aren't the first lines, but they're where I think the problem is, the lines before them are simply an enumeration called "err".
EDIT: Just a note, "list_dec.h" contains no includes, defines, ifdefs, or anything precede with a '#'. Besides the enumeration, it only contains the "list" class declaration and the "list" class member function definitions.
Generally speaking it should not, however it may be possible for there to be conflicting definitions of symbols or preprocessor macros that end up confusing the compiler. Try to narrow down the size of the problem by removing pieces and includes from the conflicting header until you can see what is causing it.
In response to the error messages you posted, the symbol NULL is often implemented as a preprocessor macro for the number 0. This is so that you can easily use it as a null pointer. Therefore this:
list(const T& NULL);
Could be converted into this syntax error by the preprocessor:
list(const T& 0);
Change the name of the parameter to something other than NULL.
Note that here:
Line 14: list(const T& NULL); (A constructor for "list" class)
NULL is the name of standard macro - when a standard header file is included before list_dec.h it will most likely cause NULL to be defined which will in turn cause your code to look something like this to the compiler:
list(const T& 0);
The constant 0 above makes the line ill-formed C++. You might get more information by instructing your compiler to produce preprocessed output file.
Presumably list_dec.h is running into a macro that's defined in those other headers (or some headers they in turn include) -- hard to say which one without seeing the first error message and the relevant part of list_dec.h!
The actual errors would give a more specific clue, bt it means there's something in your include file that is screwing up the scan for the next one. The most common thing would be some kind of unclude #-directive, like a #if missing its #endif.
If the errors are random in nature, it could be a missing semi colon. The compiler will usually halt on that, but on occasion you get "lucky".
Otherwise, conflicting names or defines. Do you have anything named std for example?