C++ error C2084: function already has a body - c++

I know this question has been asked before, but it's a clear issue in every other case. Everyone accidentally called their constructor twice. I, on the other hand, am having this issue because of prototypes in a header file, and it makes no damn sense. I'm having the error called on every single function called between these two files. Thanks!
Auto.h
#ifndef AUTO_H
#define AUTO_H
#include<string>
using std::string;
class Auto
{
public:
Auto();
Auto(const char* mk, const char* ml, int d);
void setDoors(int d);
int getDoors(void) const;
const string getMake(void) const;
const string getModel(void) const;
void setMake(const char *mk);
void setModel(const char *ml);
private:
int doors;
string make;
string model;
};
#endif
Auto.cpp
#include "Auto.h"
Auto::Auto()
{
// The strings are constructed empty by their default construtors
doors = 2;
}
Auto::Auto(const char* mk, const char* ml, int d)
{
setMake(mk);
setModel(ml);
setDoors(d);
return;
}
void Auto::setDoors(int d)
{
if (d>0)
doors = d;
else
doors = 2;
return;
}
int Auto::getDoors(void) const
{
return doors;
}
const string Auto::getMake(void) const
{
return make;
}
const string Auto::getModel(void) const
{
return model;
}
void Auto::setMake(const char *mk)
{
if (mk != 0) {
make = mk;
}
return;
}
void Auto::setModel(const char *ml)
{
if (ml != 0) {
model = ml;
}
return;
}
Error messages:
1> Lab11.cpp 1>m:\cosc1030\lab11\lab11\lab11\auto.cpp(14): error C2084: function 'Auto::Auto(void)' already has a body
1> m:\cosc1030\lab11\lab11\lab11\auto.h(18) : see previous definition of '{ctor}' 1>m:\cosc1030\lab11\lab11\lab11\auto.cpp(20): error C2084: function 'Auto::Auto(const char *,const char *,int)' already has a body
1> m:\cosc1030\lab11\lab11\lab11\auto.h(19) : see previous definition of '{ctor}'

Related

C++ Builder Pattern with Forward Declaration [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 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) {
...
}

How to fix error C2511: overloaded member function not found in C++?

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

Is this a VS2008 bug? Functional style variable initialisation

In the code below, the line
const char * const * eNames (names+cntNames);
results in a C2061 error in Visual Studio 2008:
syntax error : identifier 'identifier' -
The compiler found an identifier where it wasn't expected.
Make sure that identifier is declared before you use it.
An initializer may be enclosed by parentheses.
To avoid this problem, enclose the declarator in parentheses or make it a typedef.
This error could also be caused when the compiler detects an expression as a class
template argument; use typename to tell the compiler it is a type.
If I change to
const char * const * eNames = names+cntNames;
it doesn't complain. Is this a compiler bug? If not, why the complaint?
My About box says: Version 9.0.30729.1 SP
My colleague with GCC does not see this error.
#include <string>
#include <algorithm>
#include <functional>
#include <iostream>
namespace ns1 {
struct str_eq_to
{
str_eq_to(const std::string& s) : s_(s) {}
bool operator()(const char* x) const { return s_.compare(x)==0; }
const std::string& s_;
};
static bool getNameIndex(const char * const * names, size_t cntNames, const std::string& nm, int &result)
{
const char * const * eNames (names+cntNames); //VS2008 error C2061: syntax error : identifier 'names'
const char * const * p = std::find_if(names, eNames, str_eq_to(nm));
if(p==eNames) return false;
result = p-names;
return true;
}
} //namespace ns1
int main() {
const char * const names[] = {"Apple", "Orange","Plum"};
std::string str = "Plum";
int res;
ns1::getNameIndex(names, 3, str, res);
std::cout << str << " is at index " << res << std::endl;
return 0;
}
This is most definitely a compiler bug. Witness:
extern char** a;
typedef char* cp;
char** c(a); // error
cp* c1(a); // no error
char** c2(c1); // error
cp* n(0); // no error
char** n2(0); // error

C++ Class Methods Declaration Returns Error

I have recently started working with C++ classes and had just started when I reached an error. I have a "resource.h" file that contains the class definition of two classes: 'deck' and 'card'. I #included this file in another file, "card.cpp". In the card.cpp file I described all the methods/functions of the 'card' class. However on compilation I am getting the following the errors (fyi I am using the MinGW compiler for command-line):
card.cpp:3:29: error: ISO C++ forbids declaration of 'setCard' with no
type [-fp ermissive] card.cpp:3:1: error: prototype for 'int
Card::setCard(char, char)' does not matc h any in class 'Card'
resource.h:9:8: error: candidate is: void Card::setCard(char, char)
The "card.cpp" file:
#include "resource.h"
Card::setCard(char f, char s) {
face = f;
suit = s;
}
Card::Card (char face, char suit) {
setCard(face, suit);
}
Card::~Card () {}
The "resource.h" file:
typedef unsigned short int UINT;
class Card;
class Deck;
class Card {
public:
Card(char face, char suit);
~Card();
void setCard(char face, char suit);
char getFace() const { return face; }
char getSuit() const { return suit; }
private:
char face;
char suit;
};
class Deck {
public:
Deck();
~Deck();
Card getCard(UINT x);
private:
Card myCards[54];
};
What is causing this issue, and why in the world does the compiler think that "Card::setChard()" is an int
Card::setCard(char f, char s) {
face = f;
suit = s;
}
should be
void Card::setCard(char f, char s) {
face = f;
suit = s;
}
Some hints that helped me get to this amazing conclusion:
C++ forbids declaration of 'setCard' with no type
candidate is: void Card::setCard(char, char)
If you thought this was cryptic, hold on tight for when you get to templates. Compilers have a history of generating great error messages for them.

class issue in C++

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.