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.
Related
Below you will find my code. First I present the header file then the corresponding cpp file. The error I receive is on the line of the constructor and states "expected an identifier" I have searched everywhere to find this error and I am stuck. It is also worth noting that I am not yet comfortable with classes in c++.
The header file:
#include <Eigen/Core>
#ifndef RIDGE_GD
#define RIDGE_GD
class RIDGE_GD
{
public:
RIDGE_GD(double lambda, double eta, double max_iter);
void fit(Eigen::MatrixXd X, Eigen::MatrixXd y);
double error();
Eigen::MatrixXd cost_grad();
void gd_step();
Eigen::MatrixXd X;
Eigen::MatrixXd y;
Eigen::MatrixXd w;
private:
double m_lambda;
double m_eta;
double m_max_iter;
};
#endif
The corresponding cpp file:
#include "RIDGE_GD.h"
#include <Eigen/Core>
RIDGE_GD::RIDGE_GD(double lambda, double eta, double max_iter)
{
m_lambda = lambda;
m_eta = eta;
m_max_iter = max_iter;
}
Eigen::MatrixXd RIDGE_GD::cost_grad(RIDGE_GD.X, RIDGE_GD.y, RIDGE_GD.w, RIDGE_GD.lambda) {};
void RIDGE_GD::gd_step()
{
}
void RIDGE_GD::fit(Eigen::MatrixXd X, Eigen::MatrixXd y)
{}
double RIDGE_GD::error()
{}
Your include guard uses the same identifier as your class name. So all instances of RIDGE_GD are replaced by nothing. The compiler sees your class as
class {
public:
(double lambda, double eta, double max_iter);
// ...
};
which is why you get the error.
Use a different identifier for your include guard. (Or change the name of your class.)
The header guard is wrong, it changes the class name to nothing:
#ifndef RIDGE_GD
#define RIDGE_GD
^^ This line changes `class RIDGE_GD` to `class `, as RIDGE_GD is defined to be empty
Instead use a proper header guard:
#ifndef RIDGE_GD_HEADER_INCLUDED
#define RIDGE_GD_HEADER_INCLUDED
...
#endif
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?
//API mathAPI.h, both in Dll.cpp and Test.cpp
#ifdef __APIBUILD
#define __API __declspec(dllexport)
//#error __APIBUILD cannot be defined.
#else
#define __API __declspec(dllimport)
#endif
class math
{
public:
static __API double Pi;
static __API double Sum(double x, double y);
};
// Dll.cpp __APIBUILD is defined
#include "mathAPI.h"
double math::Pi = 3.14;
double math::Sum(double x, double y)
{
return x + y;
}
// Test.cpp __APIBUILD not defined
#include <iostream>
#pragma comment(lib, "dll.lib")
#include "mathAPI.h"
int main()
{
std::cout << math::Pi; //linker error
std::cout << math::Sum(5.5, 5.5); //works fine
return 0;
}
Error 1 error LNK2001: unresolved external symbol "public: static double Math::Pi" (?Pi#Math##2NA)
How do i get this to work?
The better solution to get your Pi value is to create a static method to init and return it, like the following in your DLL.cpp:
#include "mathAPI.h"
// math::getPi() is declared static in header file
double math::getPi()
{
static double const Pi = 3.14;
return Pi;
}
// math::Sum() is declared static in header file
double math::Sum(double x, double y)
{
return x + y;
}
This will prevent you of uninitialised value Pi, and will do what you want.
Please note that the best practice to initialize all static values/members is to initialize them in a function/method call.
Instead of exporting members one by one, export whole class. Also, I totally don't know how this code can work - you didn't provide definition for sum() (missing class scope operator) and that's what linker should complain about (instead of math::Sum(), you defined new, global sum()).
mathAPI.h
#ifdef __APIBUILD
#define __API __declspec(dllexport)
#else
#define __API __declspec(dllimport)
#endif
class __API math //Add __API to export whole class
{
public:
static double Pi;
static double Sum(double x, double y);
};
Dll.cpp
#include "mathAPI.h"
double math::Pi = 3.14;
double math::Sum(double x, double y) //You missed 'math::' here before
{
return x + y;
}
And that's it.
EDIT
You should still get an error, though. That's because you made a typo, that I haven't noticed (and you did not post the real code!). In your Dll.cpp, you wrote:
double math::Pi = 3.14;
Although you posted something different, I am sure, that your class is named Math, not math, because linker is trying to search for:
?Pi#Math##2NA
So it is looking in class Math. This is the most probable guess. Though, I am pretty sure, that you did not post the real code, but hand-written snippet.
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)
{ }
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?