data.h
namespace Data{
class Enviroment{
private:
struct EnviromentData {
float temprature;
float pressure;
float light;
};
public:
float ReturnTemprature();
float ReturnPressure();
float ReturnLight();
void SetTemprature(float);
void SetPressure(float);
void SetLight(float);
void SetAll(float, float, float);
};
};
data.cpp
#include "data.h"
struct EnviromentData envData;
float ReturnTemprature(){
return envData.temprature;
}
float ReturnPressure(){
return envData.pressure;
}
float ReturnLight(){
return envData.light;
}
void Set(float temprature, float pressure, float light){
envData.temprature = temprature;
envData.pressure = pressure;
envData.light = light;
}
On the line struct EnvirometnData envData; I have the
error Variable has incomplete type 'struct EnviromentData'
I'm not sure what I have done wrong, my implementation is based of the top answer to this stackover question
You should supply the namespace(Data::Enviroment::EnviromentData) and make EnviromentData public.
Related
I have been attempting to work with classes in c++ for the first time. My circle class and associated header file were working fine, I then moved some files and since then keep getting an error which i have displayed below.
c:\circleobje.cpp(3): error C2011: 'CircleObje' : 'class' type redefinition
c:\circleobje.h(4) : see declaration of 'CircleObje'
CircleObje.h
#ifndef CircleObje_H
#define CircleObje_H
class CircleObje
{
public:
void setCol(float r, float g, float b);
void setCoord(int x, int y);
float getR();
float getG();
float getB();
int getX();
int getY();
};
#endif
CircleObje.cpp
#include "CircleObje.h"
class CircleObje {
float rVal, gVal, bVal;
int xCor, yCor;
public:
void setCol(float r, float g, float b)
{
rVal = r;
gVal = g;
bVal = b;
}
void setCoord(int x, int y)
{
xCor = x;
yCor = y;
}
...
};
I haven't copied all of the .cpp functions as I didn't think they were relevant. These files were working without issue before I moved the file locations. Even after renaming them I still have the same error as above. Any ideas to solve the problem?
The issue is that you are defining the class twice just as the compiler is telling you. In the cpp you should provide the definitions of the functions like so:
MyClass::MyClass() {
//my constructor
}
or
void MyClass::foo() {
//foos implementation
}
so your cpp should look like:
void CirleObje::setCol(float r, float g, float b)
{
rVal = r;
gVal = g;
bVal = b;
}
void CircleObje::setCoord(int x, int y)
{
xCor = x;
yCor = y;
}
...
And all the class variables should be defined in the .h file inside of your class.
You have defined the class twice, in the header and in the cpp, so in the .cpp the compiler sees two definitions. Remove the definition of the class on the .cpp.
Class functions should be implemented in the cpp in this way:
<return_type> <class_name>::<function_name>(<function_parameters>)
{
...
}
Consider this example class:
//foo.hpp
struct foo
{
int a;
void f();
}
The class is implemented in the foo.cpp file:
#include "foo.hpp"
void foo::f()
{
//Do something...
}
you are declaring your class multiple times once in header file and another in .cpp file which is redefining your class.
CircleObje.h
#ifndef CircleObje_H
#define CircleObje_H
class CircleObje
{
public:
void setCol(float r, float g, float b);
void setCoord(int x, int y);
float getR();
float getG();
float getB();
int getX();
int getY();
public:
float rVal, gVal, bVal;
int xCor, yCor;
};
#endif
CircleObje.cpp
#include "CircleObje.h"
void CircleObje::void setCol(float r, float g, float b)
{
rVal = r;
gVal = g;
bVal = b;
}
void CircleObje::setCoord(int x, int y)
{
xCor = x;
yCor = y;
}
Remove class CircleObje {, public and the ending bracket }; and it should work. You already defined your class in the .H, thus no need to redefine it in the CPP.
Also, you should write your member implementation (in CPP file) like this :
float CircleObje::getR() { /* your code */ }
you need to put #pragma once in first line of header file then the errors will be disappears
I get "decleration does not declare anything [-fpermissive] error";
Here is my code;
#ifndef CAMERA_H
#define CAMERA_H
#include "Vector.h"
#include <string>
using namespace std;
class Camera
{
private:
int id;
float position[3];
Vector gaze;
Vector up;
float left;
float right;
float bottom;
float top;
float near;
float far;
int type;
public:
Camera(int i,string c, string g, string u, string f, string t);
int getID() {return id; }
float* getPosition() { return position; }
Vector getGaze() { return gaze; }
Vector getUp() { return up; }
float getLeft() {return left;}
float getRight() {return right;}
float getBottom() {return bottom;}
float getTop() {return top;}
float getNear() {return near;}
float getFar() {return far;}
int getType() {return type;}
};
#endif // CAMERA_H
the error starts at "float near;" and continues next 3 lines.
What is the reason of this error and how can I fix it.
Replace near and far with something else, at least for a test, e.g. near_ and far_: I suspect you are compiling with some funny header which defines near and far to be nothing. In the distant past these two were keywords used on some platforms to deal with pointers of different sizes.
If you want to verify the theory, process the source file with the -E option (you may need to remove other options from the compile line, e.g., -c): with this option the compiler produces the preprocessed output. If you capture that and look at your class there I'm quite certain that it won't contain the member names.
I suspect "near" and "far" are reserved names somewhere in "Vector.h" or string.
Renaming these variables something different should fix your problem.
I recognize that this type of question has been asked, and I looked at those responses but still think I'm missing something. I get this "No matching constructor error", because I don't have a constructor, but that being said, everything that I looked at about constructors said that you need them if you don't already include the variable names inside the class. But I already did that, so do I need a constructor? If I do, what should it look like then? I'm new to C++, taking a class, and this is for an assignment.
Here's my sensor_node.h file with the class declaration:
#ifndef SENSORNODE_H
#define SENSORNODE_H
#include <iostream>
class LOCATION {
float lat, longi, height;
public:
LOCATION (float lat, float longi, float height);
void setx(float xx);
void sety(float yy);
void setz(float zz);
void print();
};
class SensorNode {
char* NodeName;
int NodeID;
LOCATION Node1;
float batt;
int func;
public:
SensorNode(char *n, float x, float y, float z, int i, float ah);
void print();
void setOK(int o);
int getOK();
void setLOC(float longi, float lat, float h);
};
#endif /* defined(__Project_3__sensor_node__) */
And here's my main.cpp with the error (On the line that says "LOCATION"):
#include <iostream>
using namespace std;
#include "sensor_node.h"
int main() {
LOCATION a; SensorNode s1("Pulse",15.9,-30.1,0,157,2.0);
int hold;
Actually, you so have a constructor: LOCATION (float lat, float longi, float height). Since it is the only one, C++ tries to apply it. However, you did not provide any parameters, thus this constructor does not match.
You have a constructor for LOCATION (why the inconsistent capitalisation, incidentally?) that takes three floats, but the line LOCATION a tries to call the default constructor, which you haven't defined.
Hey I'm trying to pass a class object that I have made into another class to read that data. The error I'm getting is c2061: syntax error: identifier 'Player'
This is my Player2.h
#pragma once
#include "DarkGDK.h"
#include "Input.h"
#include "Player.h"
class Player2{
public:
Player2();
void PlayerSetup();
void PlayerUpdate(Player& user1);
void PlayerHealthReset();
void Gravity();
float GetPosX();
bool CheckMatchEnd();
void PlayerFire(Player& user1);
void PlayerCheckHitEnemies(Player& user1);
private:
float Vx;
float Vy;
float PosX;
float PosY;
float Speed;
int Lives;
int Health;
//
int gravity;
bool playerJumping;
bool matchEnd;
bool playerIsFiring;
float playerBullet;
bool directionBullet;
};
And the error I'm getting is that It can't recognize Player even though I brought in the Player header.
Here is Player.h
class Player{
public:
Player();
void PlayerSetup();
void PlayerUpdate(float PosX2);
void PlayerHealthReset();
float GetPosX();
float GetPosY();
void Gravity();
bool CheckMatchEnd();
void PlayerFire(float PosX2);
private:
float Vx;
float Vy;
float PosX;
float PosY;
float Speed;
int Lives;
int Health;
float playerBullet;
bool playerIsFiring;
int gravity;
bool playerJumping;
bool matchEnd;
bool directionBullet;
};
All the respective code within the header file works 100%, as I've tested it.
player does not compile before player2 is defined, so placing class player above your player2's declaration will compile player BEFORE moving onto player 2.
class player;
class player2{
//...
};
-Also as Hunter McMillen suggested think about making player 2 inherit from a base class, maybe player that defines standard methods all players would use(I dont want to steal hunter's idea, i'll let him post answer about this if he pleases with a more in depth approach).
I have a C++ class main.cpp in which I created a class like following:
class MapSearchNode
{
public:
unsigned int x; // the (x,y) positions of the node
unsigned int y;
MapSearchNode() { x = y = 0; }
MapSearchNode( unsigned int px, unsigned int py ) { x=px; y=py; }
float goalDistance( MapSearchNode &nodeGoal );
};
float MapSearchNode::goalDistance( MapSearchNode &nodeGoal )
{
float xd = fabs(float(((float)x - (float)nodeGoal.x)));
float yd = fabs(float(((float)y - (float)nodeGoal.y)));
return xd + yd;
}
int main{
//treatment
}
And it works fine but then I wanted to seperate the class MyClass, so I created a MyClass.h and MyClass.cpp and seperated the code like following:
MyClass.h
#ifndef _MAPSEARCHNODE_H
#define _MAPSEARCHNODE_H
class MapSearchNode
{
public:
MapSearchNode();
MapSearchNode( unsigned int px, unsigned int py );
public:
unsigned int x;
unsigned int y;
float goalDistance( MapSearchNode &goalNode );
};
#endif
MyClass.cpp
#include "MapSearchNode.h"
MapSearchNode::MapSearchNode():x(0), y(0))
{}
MapSearchNode::MapSearchNode( unsigned int px, unsigned int py ):x(px), y(py)
{}
float MapSearchNode::goalDistance(MapSearchNode &goalNode ){
float xDistance = fabs(float(((float)x - (float)goalNode.x)));
float yDistance = fabs(float(((float)y - (float)goalNode.y)));
return xDistance + yDistance;
}
Bur when i try to compile i have an error:
Undefined identifier goalNode;
//for the function goalDistance
Can someone please explain me why am I getting this error and how to fix it.
EDIT: I hope I haven't forgotten anything now.
EDIT:
Well thanks a lot to those who downrated. Some of us are not experts like you and it's hard for them to see errors even small ones!
On the first look there are 2 possible reasons for this error:
The most obvious one:
Your class definition is incomplete, it should be
class MyClass
{
public:
unsigned int x;
unsigned int y;
MyClass() { x = y = 0; }
MyClass( unsigned int px, unsigned int py ) { x=px; y=py; }
float Calculate( MyClass &myClass );
}; // semicolon
float MyClass::Calculate( MyClass &myClass )
{
if(x<myClass.x)
....//treatment
}
Additionally, make sure your source file (.cpp) includes your header file (.h)
So both files would look like that:
// .h file
class MyClass
{
public:
unsigned int x;
unsigned int y;
MyClass() { x = y = 0; }
MyClass( unsigned int px, unsigned int py ) { x=px; y=py; }
float Calculate( MyClass &myClass );
};
// .cpp file
#include "MyClass.h"
//definition of constructors
float MyClass::Calculate( MyClass &myClass )
{
if(x<myClass.x)
....//treatment
}