declaration does not declare anything [-fpermissive] error - c++

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.

Related

Declaration is incompatible after making the method constant

I have declared two constant functions in a header file that pop an error when I try to define them in an implementation file.
I noticed that removing "const" from the declaration gets rid of the errors but it is required by the professor that the methods are constant in the header file.
//header file
#ifndef DEGREE_RVC_H
#define DEGREE_RVC_H
#include <iostream>
using namespace std;
class degree {
public:
degree();
degree(double);
degree(double, char);
void setAll(double, char);
void setTemp(double);
void setScale(char);
double getF() const;
double getC() const;
private:
double temp;
char scale;
};
#endif
//implementation file
double degree::getC()
{
if (scale == 'c') {
return temp;
}
else return 5.0 / 9.0 * (temp - 32);
}
double degree ::getF()
{
if (scale == 'f') {
return temp;
}
else return temp * (9.0 / 5.0) + 32;
}
The getF and getC methods give me the same errors that refer me to the line they were declared in the header file, but the specific error is "declaration is incompatible with double degree::'methodname()' const"
Declarations and definitions need to match. Since you can't (and shouldn't) remove const from the declarations, add it to the definitions.
double degree::getC() const
{
...
}
double degree ::getF() const
{
...
}

Trying to use a class in one header file in another header file

I have a weightedDirectedGraph class and a vertex class in their own header file, weightedDirectedGraph.h. This is it:
#ifndef GRAPH
#define GRAPH
#include <iostream>
#include <string>
#include <vector>
#include <list>
#include "minHeapVertex.h"
using namespace std;
class vertex
{
public:
string data;
list<vertex *> neighbors;
bool known;
int distance, id;
vertex * path;
vertex(string x)
{
data = x;
}
};
class weightedDirectedGraph
{
private:
list<vertex *> vertexList;
vector<vector<int> > edgeWeights; //2D vector to store edge weights
int idCount;
weightedDirectedGraph()
{
idCount = 0;
}
vertex * findVertex(string s);
void dijkstrasAlg(vertex * s);
public:
void addVertex(string x);
//adds bi-directional edges
void addWeightedEdge(string x, string y, int weight);
};
#endif
And I have a minHeapVertex class in a minHeapVertex.h file that will be used as a priority queue in Dijkstra's algorithm. This is the file:
#ifndef MIN_HEAP_VERTEX
#define MIN_HEAP_VERTEX
#include <iostream>
#include <vector>
#include "weightedDirectedGraph.h"
using namespace std;
class minHeapVertex
{
public:
explicit minHeapVertex(int capacity = 100)
:heapArray(capacity + 1), currentSize{ 0 } {}
bool isEmpty() const
{
return (currentSize == 0);
}
vertex * getMinVertex() const; //getting C2143 error here that says I'm missing a semi-colon before '*'. Doesn't make sense though.
void insert(vertex * insertItem);
void deleteMin();
vertex * deleteAndReturnMin();
void makeEmpty()
{
currentSize = 0;
}
void decreaseKey(int index, int decreaseValue);
void remove(int index);
private:
void buildHeap();
void percolateDown(int hole);
vector<vertex *> heapArray;
int currentSize;
};
#endif
I"m getting a lot of compiling errors (with the first one being a C2143 error on the getMinVertex() declaration) and I think it may have something do with trying to access the vertex class in minHeapVertex.h. Can someone show me what I'm doing wrong? Been at it for hours, tried forward declaring the vertex class, tried removing some of the includes "", looked up the error codes and changed things, but nothing is working and just end up with a bunch of errors.
Problem:
OP has a circular dependency between minHeapVertex.h and weightedDirectedGraph.h.
Solution:
Eliminate the dependency.
minHeapVertex.h defines minHeapVertex. minHeapVertex requires vertex.
weightedDirectedGraph.h defines vertex and weightedDirectedGraph. Neither require minHeapVertex.
Three possibilities at this point:
Spin vertex off into its own vertex.h header. minHeapVertex.h and weightedDirectedGraph.h both include vertex.h and not each other.
weightedDirectedGraph.h does not require minHeapVertex.h, so remove #include "minHeapVertex.h" from weightedDirectedGraph.h to break the circle.
forward definition of class vertex; in minHeapVertex.h and the removal of #include "weightedDirectedGraph.h" from minHeapVertex.h.
Solution 1 is preferred. Giving vertex its own header may prevent future problems. 2 is easiest to implement. 3 is pretty stupid and not recommended.
Why circular dependency prevented minHeapVertex from seeing vertex:
To make this easier to see, I've removed all of the other includes from the header files.
Here's my idiotic little test.cpp
#include "weightedDirectedGraph.h"
int main(int argc, char * argsv[])
{
return 0;
}
The compiler will make a little temp file of test.cpp. It will then start parsing until it finds an include directive. The included file is copy-pasted into the temp file at the include statement. So the temp file looks sort of like this:
#define GRAPH
#include "minHeapVertex.h"
using namespace std;
class vertex
{
public:
string data;
list<vertex *> neighbors;
bool known;
int distance, id;
vertex * path;
vertex(string x)
{
data = x;
}
};
class weightedDirectedGraph
{
private:
list<vertex *> vertexList;
vector<vector<int> > edgeWeights; //2D vector to store edge weights
int idCount;
weightedDirectedGraph()
{
idCount = 0;
}
vertex * findVertex(string s);
void dijkstrasAlg(vertex * s);
public:
void addVertex(string x);
//adds bi-directional edges
void addWeightedEdge(string x, string y, int weight);
};
int main(int argc, char * argsv[])
{
return 0;
}
The compiler parses down a little further and sees the include of minHeapVertex.h and copy-pastes so you get this:
#define GRAPH
#define MIN_HEAP_VERTEX
#include "weightedDirectedGraph.h"
using namespace std;
class minHeapVertex
{
public:
explicit minHeapVertex(int capacity = 100)
:heapArray(capacity + 1), currentSize{ 0 } {}
bool isEmpty() const
{
return (currentSize == 0);
}
vertex * getMinVertex() const; //getting C2143 error here that says I'm missing a semi-colon before '*'. Doesn't make sense though.
void insert(vertex * insertItem);
void deleteMin();
vertex * deleteAndReturnMin();
void makeEmpty()
{
currentSize = 0;
}
void decreaseKey(int index, int decreaseValue);
void remove(int index);
private:
void buildHeap();
void percolateDown(int hole);
vector<vertex *> heapArray;
int currentSize;
};
using namespace std;
class vertex
{
public:
string data;
list<vertex *> neighbors;
bool known;
int distance, id;
vertex * path;
vertex(string x)
{
data = x;
}
};
class weightedDirectedGraph
{
private:
list<vertex *> vertexList;
vector<vector<int> > edgeWeights; //2D vector to store edge weights
int idCount;
weightedDirectedGraph()
{
idCount = 0;
}
vertex * findVertex(string s);
void dijkstrasAlg(vertex * s);
public:
void addVertex(string x);
//adds bi-directional edges
void addWeightedEdge(string x, string y, int weight);
};
int main(int argc, char * argsv[])
{
return 0;
}
That gets parsed down to #include "weightedDirectedGraph.h", but fortunately GRAPH has been defined, so most of weightedDirectedGraph.h gets left out. If it hadn't, Everything in weightedDirectedGraph.h would have been defined again and minHeapVertex.h would once again been included over and over and eventually the compiler would crash or tell you to expletive deleted off with a politely worded error message.
Anyway, we can already see what's gone wrong in the above code trace: minHeapVertex needs to know type vertex, but that won't be defined for another 20 lines or so.
If test.cpp had been written as
#include "minHeapVertex.h"
int main(int argc, char * argsv[])
{
return 0;
}
The header files would have been included in the other order and it would have compiled, giving a false sense of security until one day you wrote a program that included weightedDirectedGraph.h first. In other words, the library works until it doesn't, and you didn't change a line of the library's code. Have fun pulling your hair out.
Avoid circular dependencies, circular references and circular saws. All three can rip you up pretty bad.
On to using namespace std; This evil little shortcut takes EVERYTHING in the std namespace and adds it to the global namespace. If you had a function named reverse, now you have to deal with potential overload conflicts with std::reverse. The standard library is huge. There are a huge number of function, class, and variable names that are just itching to overload, override and just plain trample your stuff.
But that's your problem.
Putting using namespace std; in a header make it everyone's problem. Anyone who uses your graphing library has to wade through a minefield, and unless they take a close look at your header file and see that declaration they won't have the slightest clue.
Longer discussion can be found here. Either explicitly namespace everything (std::vector, std::string, ...) or pull in only the pieces you need and know will not conflict with your code with using. Eg:
using std::vector;
using std::string;
Do not put this in your header or someone may wind up wonder why their homebrew vector is freaking out. Probably shouldn't be homebrewing vectors, but you can't save everybody.

#include files not including (c++)

I'm having a problem with #including files in C++. When I try to compile the following code
#ifndef TILE_H_INCLUDED
#define TILE_H_INCLUDED
#include "location.h"
#include "Thing.h"
#include "Container.h"
#include <string>
using namespace std;
class Tile //VIRTUAL CLASS
{
protected:
Container onTile; //holds objects that are on the tile
Location * loc; //location
Tile* N; //links to Tiles of that direction (null if nothing)
Tile* E;
Tile* S;
Tile* W;
public:
//no constructor because superclass?
//loc can't move
//actions
bool placeOnTile(Thing * i){return onTile.addItem(i);}
//put a thing on the tile (on the floor)
Thing* takeFrmTile(int i){return onTile.movItem(i);}
//take a thing from the tile (from the floor)
Thing* access(int i) {return onTile.getItem(i);}
//gets an item, but doesn't move it (for like, flipping switches)
//direction setters/getters
void setLoc(Location* i){loc = i;}
void setN(Tile* i){N = i;}
void setE(Tile* i){E = i;}
void setS(Tile* i){S = i;}
void setW(Tile* i){W = i;}
Location* getLoc(){return loc;}
Tile* getN(){return N;}
Tile* getE(){return E;}
Tile* getS(){return S;}
Tile* getW(){return W;}
//displays
void dispOnTile(){onTile.allDisplay();}
void dispSingle(int i){onTile.singleDisplay(i);}
};
I get the error message that "Container" and "Thing" are not defined. Why is this? The #includes look to me like they are coded correctly, and they've worked in the past. I assumed it might be an issue of the #included files being not end-bracketed correctly or not using the same namespace, but they are ended correctly (with a }; ) and they are using the standard namespace. What's up? I can't find an answer and I know it's got to be something simple. For the record, the #included files are below:
#ifndef CONTAINER_H_INCLUDED
#define CONTAINER_H_INCLUDED
#include "Thing.h"
using namespace std;
class Container
{
private:
Thing ** contents; //array of pointers to Things
int numItems; //count item
int maxSize; //maxSize
public:
//constructor
Container(int i) {contents = new Thing*[i]; numItems = 0; maxSize=i;}
//sets num of items (for set-size bags)
Container() {contents = new Thing*[100]; numItems = 0; maxSize=100;}
//creates array of things
~Container() {delete contents;} //cleanup
//actions
bool addItem(Thing* th); //adds item to bag (really just rereferences the pointer)
bool rmvItem(int i); //removes item in array pos i
Thing* getItem(int i); //returns a pointer to item at array pos i
Thing* movItem(int i); //moves an item (deletes it and returns it)
//construction tools
void setMax(int i){delete contents; contents = new Thing*[i];}
//displays
void allDisplay(); //displays entire contents of container, numerated
void singleDisplay(int i); //displays content item i
};
#endif // CONTAINER_H_INCLUDED
#ifndef LOCATION_H_INCLUDED
#define LOCATION_H_INCLUDED
#include <string>
#include <sstream>
#include "Tile.h"
using namespace std;
class Location //stores xy coordinates of something
{
int x; //0 is NOT on map
int y;
Tile* ti; //Locations contain pointers to tiles
public:
//constructors (mainly for debug)
Location(){x=y=0;} //put object OUT OF MAP
Location(int ix, int iy){x=ix;y=iy;} //put object AT loc on map
//setters
void setX(int ix){x=ix;} //sets x
void setY(int iy){y=iy;} //sets y
void setT(Tile*i){ti=i;} //sets Tile
//getters
int getX() {return x;}
int getY() {return y;}
string getloc() //return location as a string, separated by a comma
{
ostringstream locxy; //create stringstream obj to handle input
locxy << getX() << "," << getY() << ". "; //put x, space, y into stringstream
string locret = locxy.str(); //convert stringstream to string
return locret; //return string
}
};
#endif // LOCATION_H_INCLUDED
#ifndef THING_H_INCLUDED
#define THING_H_INCLUDED
#include <string>
#include "location.h"
using namespace std;
class Thing //superclass that will be the base for objects
{
protected:
Location * loc; //location (in or out of map)
string name; //name
string desc; //description
bool deletable; //deletable (for undestructible items)
bool takeable; //if you can put it in your inv
bool hasInv; //returns true if the item has an inventory
public:
//constructor/destructor (debug only)
Thing() //sets initial values
{loc = new Location(0, 0);
name = "Uninitialized";
deletable = takeable = true;
}
Thing(int ix, int iy) //sets location
{loc = new Location(ix, iy);
name = "Uninitialized";
deletable = takeable = false;}
~Thing() {delete loc;} //deletes allocated data
//getters
Location* getLoc() {return loc;} //returns the location
string getDesc(){return desc;} //returns the description
bool getDel(){return deletable;} //returns deletable status
bool getTake(){return takeable;} //returns takeable status
string getName(){return name;} //returns name
string dispLoc(){return loc->getloc();} //displays location
//setters
void setName(string s){name = s;} //sets name
void setDel(bool b){deletable = b;} //sets deletability
void setDesc(string d) {desc = d;} //sets desc
void setLoc(Location* l) {loc = l;} //sets loc
void setTake(bool b){takeable = b;} //sets takeability
//accessors
};
#endif // THING_H_INCLUDED
I believe this is because you have recursive dependincies. That is, your classes all depend on each other, which means that at some point one of the classes will not be able to compile, because in order to get compiled it will need declaration of a class, but, it will not be able to find the declaration, because it is in the header file that is already up there in the stack of "#include" and thus because of guarding "#ifdef" becomes empty.
To give you the example.
To compile Tile, you need declaration of Location, so, naturally, you #include "location.h". But, to compile declaration of Location, you need declaration of Tile, so, you #include "Tile.h". But Tile.h has been #include-ed already, so there is no declaration for it!
The way to fight such circular dependencies is to use incomplete class declaration. For example, instead of including location.h into Tile.h, write
class Location;
class Tile
{
Location* loc;
}
This works as long as Location is only used to declare a pointer or reference and no members of Location class are accessed.
Then, in you 'Tile.c' file you can #include "location.h" and allow your inplementation of Tile methods access to Location members.
You have an include-loop
Tile.h includes
location.h
Thing.h
Container.h
Container.h includes Thing.h
Thing.h includes location.h
location.h includes Tile.h
Oops.
So let us say in the .cpp, the first to be included is Container.h. Then that includes Thing.h before it declares anything. Thing.h in turn includes location.h before it declares anything. Then that includes Tile.h before it declares anything. In Tile.h, all the include guards make the recursive includes no-ops.
And thus in Tile.h, Thing and Container are unknown.

"No matching constructor for initialization of. . ."

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.

Trouble passing an object as a parameter

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).