I have this in furniture.h:
#include <iostream>
#include <string>
using namespace std;
class Furniture {
public:
Furniture();
virtual ~Furniture();
void setname(string name);
void setprice(double price);
int getprice();
string getname();
private:
string name;
int price;
protected:
static int NumberOfItems;
int Id;
}
and this in furniture.cpp
#include "furniture.h"
void Furniture::setname(string name) {
this->name = name;
}
string Furniture::getname()
{
return this->name;
}
void Furniture::setprice(double price) {
this->price = price;
}
int Furniture::getprice() {
return this->price;
}
int main() {
Furniture *model = new Furniture();
model->setname("FinalDestiny");
model->setprice(149.99);
cout<<"Model name: "<<model->getname()<<" - price = "<<model->getprice();
}
But I get some errors like:
Error 1 error C2628: 'Furniture' followed by 'void' is illegal (did you forget a ';'?) c:\final\facultate\poo\laborator 1\furniture.cpp 3 1 POO_lab
Error 2 error C2556: 'Furniture Furniture::setname(std::string)' : overloaded function differs only by return type from 'void Furniture::setname(std::string)' c:\final\facultate\poo\laborator 1\furniture.cpp 3 1 POO_lab
Error 3 error C2371: 'Furniture::setname' : redefinition; different basic types c:\final\facultate\poo\laborator 1\furniture.cpp 3 1 POO_lab
Error 5 error C2264: 'Furniture::setname' : error in function definition or declaration; function not called c:\final\facultate\poo\laborator 1\furniture.cpp 19 1 POO_lab
What am I doing wrong?
You are missing a ; at the end of the class definition in your header file.
// ...snipped...
protected:
static int NumberOfItems;
int Id;
}; // <-- here
You've forgotten a semicolon at the end of your class definition.
// ...
protected:
static int NumberOfItems;
int Id;
}; // <--
I hate that about C++ :)
Two things;
You're not ending your class definition with a ;, you need one at the end of furniture.h.
You've declared that there's a constructor and destructor, but neither is implemented in your .cpp file.
Related
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 6 years ago.
Improve this question
I recently got stuck in a situation. In the first version, everything is implemented in header file and it works fine. In the second version when i tried to separate implementation from header declarations, I got many errors. In the below lines, i m going to demonstrate problem. Thanks in advance..
First Version (it works fine!)
cameravalue.h
#ifndef CAMERAVALUE_H
#define CAMERAVALUE_H
#include <string>
class CameraValue
{
private:
class CameraProperties
{
private:
CameraProperties()
: mId(-1),
mName(),
mAddress(),
mExposure(),
mFocus()
{}
int mId;
std::string mName;
std::string mAddress;
std::string mExposure;
long long mFocus;
friend class CameraValue;
friend class CameraBuilder;
};
public:
class CameraBuilder
{
public:
CameraBuilder(int id)
{
mProperties.mId = id;
}
CameraBuilder& setName(std::string& name)
{
mProperties.mName = name;
return *this;
}
CameraBuilder& setAddress(std::string& adress)
{
mProperties.mAddress = adress;
return *this;
}
CameraBuilder& setExposure(std::string& exposure)
{
mProperties.mExposure = exposure;
return *this;
}
CameraBuilder& setFocus(int focus)
{
mProperties.mFocus = focus;
return *this;
}
CameraValue build()
{
return CameraValue(mProperties);
}
private:
CameraProperties mProperties;
};
private:
CameraValue(const CameraProperties& properties)
:mProperties(properties)
{}
CameraProperties mProperties;
};
#endif // CAMERAVALUE_H
main.cpp
#include "cameravalue.h"
#include <iostream>
int main(int argc, char *argv[])
{
CameraValue cm = CameraValue::CameraBuilder(1).setName(std::string("Huseyin")).build();
return 0;
}
Second Version (Don't work)
cameravalue.h
#ifndef CAMERAVALUE_H
#define CAMERAVALUE_H
#include <string>
class CameraValue
{
private:
class CameraProperties;
public:
class CameraBuilder;
private:
CameraValue(const CameraProperties& properties);
CameraProperties mProperties;
};
#endif // CAMERAVALUE_H
cameravalue.cpp
#include "cameravalue.h"
#include <string>
class CameraValue::CameraProperties
{
private:
CameraProperties()
: mId(-1),
mName(),
mAddress(),
mExposure(),
mFocus()
{}
int mId;
std::string mName;
std::string mAddress;
std::string mExposure;
long long mFocus;
friend class CameraValue;
friend class CameraBuilder;
};
class CameraValue::CameraBuilder
{
public:
CameraBuilder(int id)
{
mProperties.mId = id;
}
CameraBuilder& setName(std::string& name)
{
mProperties.mName = name;
return *this;
}
CameraBuilder& setAddress(std::string& adress)
{
mProperties.mAddress = adress;
return *this;
}
CameraBuilder& setExposure(std::string& exposure)
{
mProperties.mExposure = exposure;
return *this;
}
CameraBuilder& setFocus(int focus)
{
mProperties.mFocus = focus;
return *this;
}
CameraValue build()
{
return CameraValue(mProperties);
}
private:
CameraProperties mProperties;
};
CameraValue::CameraValue(const CameraProperties& properties)
: mProperties(properties)
{}
main.cpp
#include "cameravalue.h"
#include <iostream>
int main(int argc, char *argv[])
{
CameraValue cm = CameraValue::CameraBuilder(1).setName(std::string("Huseyin")).build();
return 0;
}
Compile Errors
cameravalue.cpp
c:\users\huseyin\documents\builderpattern\cameravalue.h(20) : error
C2079: 'CameraValue::mProperties' uses undefined class
'CameraValue::CameraProperties' ..\BuilderPattern\cameravalue.cpp(74)
: error C2440: 'initializing' : cannot convert from 'const
CameraValue::CameraProperties' to 'int'
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
..\BuilderPattern\cameravalue.cpp(74) : error C2439:
'CameraValue::mProperties' : member could not be initialized
c:\users\huseyin\documents\builderpattern\cameravalue.h(20) : see declaration of 'CameraValue::mProperties'
c:\users\huseyin\documents\builderpattern\cameravalue.h(20) : error
C2079: 'CameraValue::mProperties' uses undefined class
'CameraValue::CameraProperties' ..\BuilderPattern\main.cpp(9) : error
C2440: '<function-style-cast>' : cannot convert from 'int' to
'CameraValue::CameraBuilder'
Source or target has incomplete type ..\BuilderPattern\main.cpp(9) : error C2228: left of '.setName' must
have class/struct/union ..\BuilderPattern\main.cpp(9) : error C2228:
left of '.build' must have class/struct/union
..\BuilderPattern\main.cpp(9) : error C2512: 'CameraValue' : no
appropriate default constructor available
..\BuilderPattern\main.cpp(10) : error C2039: 'getName' : is not a
member of 'CameraValue'
c:\users\huseyin\documents\builderpattern\cameravalue.h(8) : see declaration of 'CameraValue'
Definition of class CameraBuilder should be visible in main.cpp, so you can't just forward-declare it in cameravalue.h. But you can make the definitions of its member functions out-of-line:
// cameravalue.h
class CameraValue {
class CameraBuilder {
public:
CameraBuilder(int id);
...
};
};
// cameravalue.cpp
CameraValue::CameraBuilder::CameraBuilder(int id) {
...
}
I am getting flustrated with two errors and I have absolutely no idea what is wrong.
#ifndef ListElements
#define ListElements
#include "RentalObjects.h"
struct RentalList{
ObjectBase* content;
RentalList* Next;
};
#endif
All the time I get this error:
Error 1 error C2143: syntax error : missing ';' before '*'
Error 2 error C4430: missing type specifier - int assumed.
The RentalObjects.h file features a declaration of the ObjectBase class, which looks as follows:
class ObjectBase{
protected:
char Make[16];
char Model[16];
int Year;
float PricePerDay;
Booking* Availability;
public:
void SetMake(char* value);
void SetModel(char* value);
void SetYear(int value);
void SetPrice(float value);
bool DisposeBookings();
bool Book(int Start,int End);
char* GetMake();
char* GetModel();
int GetYear();
float GetPrice();
~ObjectBase();
};
I'd be grateful for a tip.
When declaring pointers or references, you don't need the whole class/struct definition.
Instead of:
#include "RentalObjects.h"
struct RentalList {
ObjectBase* content;
RentalList* Next;
};
you could do:
class ObjectBase;
struct RentalList {
ObjectBase* content;
RentalList* Next;
};
this could get you out of a circular include, which might be what's causing your broblem
I have a problem to pass my own class as a parameter.
Here is my codes:
PayloadContainer.h
namespace Project
{
namespace C
{
namespace Helper
{
class PayloadItem
{
public:
string Key;
string Value;
char Type;
char Mode;
char IsArray;
int FieldCount;
char FieldType;
int RowCount;
};
class PayloadContainer
{
public:
PayloadContainer( const char *Command );
PayloadContainer(void);
~PayloadContainer(void);
public:
vector<PayloadItem> PayloadItems;
};
}
}
}
ParseBinary.h
namespace Project
{
namespace C
{
namespace Helper
{
class ParseBinary
{
public:
ParseBinary(void);
~ParseBinary(void);
private:
void WriteRequestToBinary( const char *BinFileName );
void WriteRequestRecord( unsigned char file[], PayloadItem& item, string charSet );
};
}
}
}
ParseBinary.cpp
namespace Project
{
namespace C
{
namespace Helper
{
void ParseBinary::WriteRequestToBinary( const char *BinFileName )
{
unsigned char in;
// Do Something
for (auto &item : _payload->PayloadItems)
{
// Do Something
WriteRequestRecord( in, (Helper::PayloadItem)item, _payload->CharSet );
}
}
void ParseBinary::WriteRequestRecord( unsigned char file[], PayloadItem& item, string charSet )
{
-----> here, I get "error C2511: overloaded member function not found".
}
}
}
}
What I am trying to do is iterating vector where PayloadItem is my own class, and pass that class to a function.
But, when I build it I get this error C2511: overloaded member function not found error.
Please tell me where to fix this.
EDIT
This is the error message.
Error 9 error C2061: syntax error : identifier 'PayloadItem' c:\webdev\Project.c.helper\ParseBinary.h 46 1 Project.C.Helper
error C2511: overloaded member function not found in 'Project::C::Helper::ParseBinary' c:\webdev\Project.c.helper\ParseBinary.cpp 958 1 Project.C.Helper
Got an error on my C++ program. It is likely to be something simple as I have only just started programming.
The error is:
Error 1 error C2511: 'void BMI::getWeight(double)' : overloaded member function not found in 'BMI' c:\users\**********\documents\visual studio 2012\projects\project2\project2\bmi.cpp 40 1 Project2
bmi.h:
#include <iostream>
#include <string>
using namespace std;
#ifndef BMI_H
#define BMI_H
class BMI {
public:
//Defualt Constructor
BMI();
//Overload Constructor
BMI(string, int, double);
//Destructor
~BMI();
//Accessor Functions
string getName() const;
// getName - returns name of paitent
int getHeight() const;
//getHeight - returns height of paitent
double getWeight() const;
//getWeight returns weight of paitent
private:
//Member Variables
string newName;
int newHeight;
double newWeight;
};
#endif
bmi.cpp:
// Function Definitions
#include "BMI.h"
BMI::BMI() {
newHeight = 0;
newWeight = 0.0;
}
BMI::BMI(string name, int height, double weight) {
newName = name;
newHeight = height;
newWeight = weight;
}
BMI::~BMI() {
}
string BMI::getName() const {
return newName;
}
int BMI::getHeight() const {
return newHeight;
}
double BMI::getWeight() const {
return newWeight;
}
void BMI::setName(string name) {
newName = name;
}
void BMI::setHeight(int height) {
newHeight = height;
}
void BMI::setWeight(double weight) {
newWeight = weight;
}
Ok, when I try to compile the code I see a couple of problems:
The setName(string) function in the .cpp doesn't match anything in the header.
The setHeight(int) function in the .cpp doesn't match anything in the header.
The setWeight(double) function in the .cpp doesn't match anything in the header.
I would try to solve the compilation errors in the order they occur, and then see if you still have a problem with getWeight. I'm assuming that you are seeing the same problems with the undeclared functions that I'm seeing.
The error appears to be telling you that you are trying to call BMI::getWeight() somewhere and you are passing in it a parameter with a double type. This error is a bit perplexing as no such function that matches void BMI::getWeight(double) defined in either the BMI class in the header file or the cpp file. If you have changed the code since you posted it up then please do update and post ALL of the compiler messages. I suspect that you have not posted all of the compiler messages because SetName,setHeight and setWeight are all missing from the BMI class definition. So make sure you add all of those into the BMI class.
Also I think that it's good practice to initialize your data members differently. So instead of:
BMI::BMI(string name, int height, double weight) {
newName = name;
newHeight = height;
newWeight = weight;
}
you should prefer:
BMI::BMI(string name, int height, double weight):
newName(name),
newHeight(height),
newWeight(weight)
{ }
I started learning C++, classes, objects, structures and more, but I'm having some problems with this.
#include <iostream>
using namespace std;
class Owner
{
public:
// Getters
string GetName(){return info.name;}
int GetAge(){return info.age;}
short int GetGender(){return info.gender;}
// Setters
void SetName(string value){info.name = value;}
void SetAge(int value){info.age = value;}
void SetGender(short int value){info.gender = value;}
private:
struct info
{
string name;
int age;
short int gender;
};
};
class Pet
{
public:
// Getters
string GetName(){return info.name;}
int GetAge(){return info.age;}
short int GetGender(){return info.gender;}
// Setters
void SetName(string value){info.name = value;}
void SetAge(int value){info.age = value;}
void SetGender(short int value){info.gender = value;}
private:
struct info
{
string name;
int age;
short int gender;
}
};
int main()
{
// Creating object ...
cout << "qq" << endl;
return 0;
}
But I get these errors when I try to compile it:
In member function 'std::string Owner::GetName()':|
main.cpp|9|error: expected primary-expression before '.' token|
In member function 'int Owner::GetAge()':|
main.cpp|10|error: expected primary-expression before '.' token|
In member function 'short int Owner::GetGender()':|
main.cpp|11|error: expected primary-expression before '.' token|
In member function 'void Owner::SetName(std::string)':|
main.cpp|14|error: expected unqualified-id before '.' token|
In member function 'void Owner::SetAge(int)':|
main.cpp|15|error: expected unqualified-id before '.' token|
In member function 'void Owner::SetGender(short int)':|
main.cpp|16|error: expected unqualified-id before '.' token|
main.cpp|45|error: expected unqualified-id before '}' token|
In member function 'std::string Pet::GetName()':|
main.cpp|30|error: expected primary-expression before '.' token|
In member function 'int Pet::GetAge()':|
main.cpp|31|error: expected primary-expression before '.' token|
In member function 'short int Pet::GetGender()':|
main.cpp|32|error: expected primary-expression before '.' token|
In member function 'void Pet::SetName(std::string)':|
main.cpp|35|error: expected unqualified-id before '.' token|
In member function 'void Pet::SetAge(int)':|
main.cpp|36|error: expected unqualified-id before '.' token|
In member function 'void Pet::SetGender(short int)':|
main.cpp|37|error: expected unqualified-id before '.' token|
||=== Build finished: 13 errors, 0 warnings ===|
Why does it give me so many errors?
I don't know why, because it is obvious that, for example,
string GetName()
{
return info.name;
}
returns a string, from the structure info.name
I'm using CodeBlocks.
You're declaring the struct as a type (Owner.info) instead of as a member (this->info). You probably want this:
struct OwnerInfo
{
string name;
int age;
short int gender;
};
class Owner {
// stuff..
private:
OwnerInfo info;
};
Or, the more reasonable version would be just having them there directly instead of inside a pointless struct:
class Owner {
// stuff..
private:
string name;
int age;
short int gender;
};
You're misunderstanding the syntax of the struct keyword, furthermore the actual member variable has to be declared before the member functions accessing it. So change your class declarations to something like
class Owner
{
private:
struct
{
string name;
int age;
short int gender;
} info;
public:
// Getters
string GetName(){return info.name;}
int GetAge(){return info.age;}
short int GetGender(){return info.gender;}
// Setters
void SetName(string value){info.name = value;}
void SetAge(int value){info.age = value;}
void SetGender(short int value){info.gender = value;}
};
The declaration:
private:
struct info
{
string name;
int age;
short int gender;
};
... defines the layout of a nested structure type info, much like the definition of the entire class does. However, info is now a type nested within Owner, not an instance of that type that is a member of Owner. Instead, try naming the struct Info, then declaring Info info = new Info(); in the private section of Owner.
Well you created a struct and therefor told your compiler "info" is a typ with the following attributes...
You need to declare a variable of the type "info".
info personalInfo;
Declare it as class member and you can create your Get-er and Set-er.
string GetName(){return personalInfo.name;}
More Information
You should create a object of struct info. You just cannot access a field of a struct directly without creating an object.. Why do you need the struct at all? try this
class Owner
{
private:
string name;
int age;
short int gender;
public:
// Getters
string GetName(){return this->name;}
int GetAge(){return this->age;}
short int GetGender(){return this->gender;}
// Setters
void SetName(string value){this->name = value;}
void SetAge(int value){this->age = value;}
void SetGender(short int value){this->gender = value;}
};