Why am I getting this redefinition of class error? - c++

Apologies for the code dump:
gameObject.cpp:
#include "gameObject.h"
class gameObject
{
private:
int x;
int y;
public:
gameObject()
{
x = 0;
y = 0;
}
gameObject(int inx, int iny)
{
x = inx;
y = iny;
}
~gameObject()
{
//
}
int add()
{
return x+y;
}
};
gameObject.h:
class gameObject
{
private:
int x;
int y;
public:
gameObject();
gameObject(int inx, int iny);
~gameObject();
int add();
};
Errors:
||=== terrac, Debug ===|
C:\terrac\gameObject.cpp|4|error: redefinition of `class gameObject'|
C:\terrac\gameObject.h|3|error: previous definition of `class gameObject'|
||=== Build finished: 2 errors, 0 warnings ===|
I can't figure out what's wrong. Help?

You're defining the class in the header file, include the header file into a *.cpp file and define the class a second time because the first definition is dragged into the translation unit by the header file. But only one gameObject class definition is allowed per translation unit.
You actually don't need to define the class a second time just to implement the functions. Implement the functions like this:
#include "gameObject.h"
gameObject::gameObject(int inx, int iny)
{
x = inx;
y = iny;
}
int gameObject::add()
{
return x+y;
}
etc

add in header files
#pragma once

the implementation in the cpp file should be in the form
gameObject::gameObject()
{
x = 0;
y = 0;
}
gameObject::gameObject(int inx, int iny)
{
x = inx;
y = iny;
}
gameObject::~gameObject()
{
//
}
int gameObject::add()
{
return x+y;
}
not within a
class gameObject
{
}
definition block

You should wrap the .h file like so:
#ifndef Included_NameModel_H
#define Included_NameModel_H
// Existing code goes here
#endif

You're defining the same class twice is why.
If your intent is to implement the methods in the CPP file then do so something like this:
gameObject::gameObject()
{
x = 0;
y = 0;
}
gameObject::~gameObject()
{
//
}
int gameObject::add()
{
return x+y;
}

If you are having issues with templates or you are calling the class from another .cpp file
try using '#pragma once' in your header file.

Either try adding #pragma once at the top of your file, or the old way... Place this at the top of your code
#ifndef GAME_H //ensuring that this object is only initialized once
#define GAME_H
and this below the last line
#endif
Which will ensure only a single initialization of the class.

Include a few #ifndef name #define name #endif preprocessor that should solve your problem.
The issue is it going from the header to the function then back to the header so it is redefining the class with all the preprocessor(#include) multiple times.

You define the class gameObject in both your .cpp file and your .h file.
That is creating a redefinition error.
You should define the class, ONCE, in ONE place.
(convention says the definition is in the .h, and all the implementation is in the .cpp)
Please help us understand better, what part of the error message did you have trouble with?
The first part of the error says the class has been redefined in gameObject.cpp
The second part of the error says the previous definition is in gameObject.h.
How much clearer could the message be?

Related

C++ Using a reference function from header file in the cpp?

I have a header file and a cpp file. In the .h file I declared a class and a function with returning a reference:
.h file:
#include <iostream>
class testclass {
Car &createCar(int x, int y);
}
.cpp file:
#include<testclass.h>
Car testclass:&createCar(int x, int y)
{
....
}
But when I try to access the function in the .cpp file I get an error:
Declaration is not compatible
The function definition in .cpp file should be compatible with its decleration in .h file so modify it as follows.
.cpp file:
#include"testclass.h"
Car& testclass::createCar(int x, int y)
{
....
}
Note that I modified <testclass.h> to "testclass.h". use the brackets only with the built in headers.
Follow the following line if you want to know why you should use "testclass.h" instead of <testclass.h> and which one of them you should use Link
In your .h file, & is not a reference to a function. The & is part of the function's return type. So, the function actually returns a reference to a Car instance. It would help you to understand it if you actually write it that way:
Car& createCar(int x, int y);
As such, in the .cpp file, you need to move the & to the return type to match the declaration:
#include <iostream>
class testclass {
Car& createCar(int x, int y);
};
#include "testclass.h"
Car& testclass::createCar(int x, int y)
{
....
}

'CalucateNumbers' is missing exception specification 'noexcept'

I'm a beginner with C++
I'm having a trouble when I set the header class values.
CalucateNumbers::CalucateNumbers() {
ResetValues();
}
void CalucateNumbers::ResetValues() {
firstNumber = 0;
secondNumber = 8;
}
CalucateNumber is missing exception specification noexcept
Help please?
This is the C plus plus Code file with the name FBullCowGame.cpp
#include "FBullCowGame.hpp"
FBullCowGame::FBullCowGame() {
Reset();
}
void FBullCowGame::Reset() {
CurrentTries = 0;
MaxTries = 8;
}
This is the header file with the name FBullCowGame.hpp
#ifndef FBullCowGame_hpp
#define FBullCowGame_hpp
#include <stdio.h>
#include <string>
#endif /* FBullCowGame_hpp */
class FBullCowGame {
public:
void Reset(); // TODO Make a reset void
// Not important.., The important is this ^^
private:
int CurrentTries;
int MaxTries;
};
Here is the MCVE on godbolt.
You were incorrect when you said "Yes it does" when asked whether the definition matches the header. It does not match the header, because it's not even present in the header!
Your class FBullCowGame doesn't declare a custom constructor, so the compiler created a default one. You then try to create a custom one, and the compiler thinks you're trying to implement the default constructor (which happens to be noexcept), so it says "This redeclaration doesn't match the implicit declaration."
Your real problem is that you forgot to tell the compiler "I'm going to give this class a custom constructor."
class FBullCowGame {
public:
FBullCowGame(); // <----- you forgot this
void Reset(); // TODO Make a reset void
// Not important.., The important is this ^^
private:
int CurrentTries;
int MaxTries;
};
(You also have a problem with the scope of the #ifdef guard in your header file.)
That's a very misleading error message. The problem is that the class definition does not declare a default constructor, but the source code attempts to implement one. To fix this, add a declaration for the default constructor to the class definition.

Incomplete/Undefined type when using nested classes

I have been attempting to make some code, but I am a bit new to c++ and need some help.
I cannot instantiate class Player as a pointer, because it's an "incomplete type" (or undefined type, vs says both). Below are some (simplified, albeit not very) versions of my code:
Entity.h
#pragma once
#include <vector>
class Entity
{
public:
static void init();
class EntityObject;
class Player;
static std::vector<EntityObject*> entities;
};
Entity.cpp
#include "Entity.h"
void Entity::init()
{
entities = std::vector<EntityObject*>();
}
class Entity::EntityObject
{
private:
float velX, velY, x, y;
public:
EntityObject(float xa, float ya) { x = xa; y = ya; }
float getVelX() { return velX; }
float getVelY() { return velY; }
float getX() { return x; }
float getY() { return y; }
};
class Entity::Player : EntityObject
{
public:
Player(float xa, float ya) : EntityObject(xa, ya)
{
printf("Player created");
}
};
Can anyone tell me why
#include "Entity.h"
int main(int argc, char* argv)
{
Entity::init();
Entity::EntityObject* player = new Entity::Player(10.0f, 10.0f);
Entity::entities.push_back(player);
}
gives an incomplete/undefined type?
Thanks.
Edit:
The errors are:
Both errors direct to this line: Entity::EntityObject* player = new Entity::Player(10.0f, 10.0f);
Error (active) E0070 incomplete type is not allowed
Error C2027 use of undefined type 'Entity::Player'
You defined the Entity::Player class in the .cpp file, not in the .h file. Therefore, even though the main() includes the .h file, it does not know about Entity::Player.
Entity::Player is forward declared in Entity.h.
When the compiler compiles your main.cpp module, it does not know anything about this class except that it exists, in particular it has no idea that this class as a constructor taking two float : Player(float xa, float ya)
=> Your issue is related to forward declaring, not nested class.
Read this thread to understand your problem
What are forward declarations in C++?
Read this one to understand what you can and what you can't do with forward declaration
When can I use a forward declaration?

C++.Passing to functions.Syntax issue

I am pursuing some interest in c++ programming by way of self instruction. I am working on some basic stuff for now and am currently having issue getting my classes talking/instantiated?.
I am trying to get my main cpp file to compile alongside a header and call to some class functions through the main using a more efficient command method.
I am stuck and would appreciate some help. I will include both files. I am just trying to get a return value from the header by calling the function.
error:
main.cpp:6.21 error: cannot call member function 'void myClass::setNumber(int) without object
the code works when compiled with the main, so it is something with the 'scope resolution operator' i think. First is main.cpp
#include <iostream>
#include "myClass.h"
using namespace std;
int main(){
myClass::setNumber(6);
{
return number;
}
}
Then my header file myClass.h
// MyClass.h
#ifndef MYCLASS_H
#define MYCLASS_H
class myClass {
private:
int number;//declares the int 'number'
float numberFloat;//declares the float 'numberFloat
public:
void setNumber(int x) {
number = x;//wraps the argument "x" as "number"
}
void setNumberFloat(float x) {
numberFloat = x;
}
int getNumber() {//defines the function within the class.
number += 500;
return number;
}
float getNumberFloat() {//defines the function
numberFloat *= 1.07;
return numberFloat;
}
};
#endif
Any help?
The error message says everything:
cannot call member function 'void myClass::setNumber(int)' without object
You need to create an object first:
myClass obj;
then call the class method on that object:
obj.setNumber(6);
The value 6 will get assigned to the number field of the obj variable.

Class definition in header file issues

I am receiving the following error from the compiler
./include.list.h(22) error: identifier "input" is undefined.
Here is the header file where I explicitly define input-
#ifndef INIT_H
#define INIT_H
#include "list.h"
class input{
public:
//** thermo variables **//
int N;
double nddensity;
double ndtemp;
double ndvol;
double ndside;
double ndsideh;
// ** force field variables** //
double eps;
double sigma;
double rcut;
double rv;
double rcut2;
double rv2;
double rcrv2;
input();
};
void print(input &);
double randomnumber();
void position(list &, input &, int flag);
#endif
In init.cpp, I have the class initialization:
input:: input() {
//** thermo variables **//
N = 500;
nddensity =.8;
ndtemp = 2.0;
ndvol = N/nddensity;
ndside = pow(ndvol,1.0/3.0);
ndsideh = ndside/2;
// ** force field variables** //
eps = 1;
sigma = 1;
rcut = 2.5;
rv = 1.1*rcut;
rcut2 = rcut*rcut;
rv2 = rv*rv;
rcrv2 = (rv-rcut)*(rv-rcut);
}
I can't seem to figure out why input would be undefined to void print. Any help is appreciated.
You have a circular dependency.
You can forward declare the other class, but this is still a poor design. Better to just do away with the circular dependency altogether. At second glance, do you even need to include list.h? Where are you using anything declared in it?
You include list.h before the class definition, so it's not available in that header. According to the error message, something in list.h needs a declation of the class.
You don't need a full definition of list here, so replace the #include with a forward declaration
class list;
Within list.h, you probably need a similar declaration of input; or it might be necessary to include init.h if the header does something complicated with it.