// q.h file
#ifndef __Q_H__
#define __Q_H__
using namespace std;
#include "n.h"
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <string>
class Q
{
public:
Q();
private:
N* beginning; //error N does not name a type
N* end; //error N does not name a type
int count;
};
#endif // end of file
// q.cpp file
#include "q.h"
#include "n.h"
#include <iostream>
using namespace std;
#include<string>
Q::Q()
{
beginning = NULL;
end = NULL;
}
// n.h file
#ifndef _N_H__
#define _N_H__
using namespace std;
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <string>
#include "q.h"
class N
{
public:
N(int);
// next is a pointer object of type N
N* next;
// memeber for Node class
int value;
};
#endif
// n.cpp file
#include<string>
#include "q.h"
#include "n.h"
N::N(int v)
{
value = v;
}
I get an error at the following lines below. I tried changing user namespace std; to the first line of each file but that still does not work. I've also tried changing the order of q.h and n.h in which they are presented but still nothing.
N* beginning;
N* end;
Also I know one great solution is by using "forward declare" but this is part of a test file that i given to me by someone so I cannot do a forward declaration of a class.
So if anyone can please just take a look and see how I can fix this.
Thanks.
You have a circular reference (q.h includes n.h and n.h includes q.h). This error occurs because the compiler is including (maybe because the main program) q.h before than n.h. Thus, q.h need to include an empty class declaration as follows to Q knows about N.
...
#include <string>
class N;
class Q
{
...
If you can't edit those files, you still can add the empty class declaration anywhere before the "Q" class declaration (e.g. before including q.h in your main program). However, the class N don't need anything from Q, so you don't need to include q.h in n.h.
Since #include's are done in a way, where the preprocessor just takes the file that you are #include'ing and copy-pastes in the spot of #include statement, when n.h is being processed, it includes q.h, and then class Q appears before class N. Hence, it doesn't know what the class N is, since the compiler works in linear fashion (from the start of the file to the end).
So, it is my suggestion, to remove #include "q.h" statement from n.h, since the class N doesn't use class Q.
Related
I am attempting to make a class to contain some math operations from a CRC math tables handbook I have, in creating one of the functions I got a strange error I had not seem before. The code for both the cpp and the header are below:
//Header File
#include <iostream>
#include <cmath>
#include <string>
#define int "CRCMathLib_H"
using namespace std;
class CRCMathLib
{
public:
int DoReturn_Totient(int Toter); //Error comes from here when trying to declare as an int
};
//CPP Class File
#include "CRCMathLib.h"
#include <iostream>
#include <cmath>
#include <string>
using namespace std;
int CRCMathLib::DoReturn_Totient(int Toter)
{
return 0;
}
//CPP Main File
#include <iostream>
#include <cmath>
#include <string>
#include "CRCMathLib.h"
using namespace std;
int main()
{
return 0;
}
The Main file does not do anything as of yet as this is a completely new file for these operations, I believe this may be a preprocessing error and its not picking up on the int statement as I ran it on another PC with VS and it was able to read the statement. anything would help. Also it was requesting a decleration of the header file, so thats why I placed the int there, is this possibly the issue? removing it returns the error of not having a decleration.
In your .h remove #define int "CRCMathLib_H" which is most probably a typo
replace it by
#include <iostream>
#include <cmath>
#include <string>
#pragma once
The #pragma once ensure you can safely include your .h from the cpp implementation file and the main.cpp
You mis understood include guard protection usually done by
ifndef CRCMathLib_H
#define CRCMathLib_H
// all of you .h file delcaration
#endif
This can be easily replace by the #pragma once statement at the begining of the file
More on this here: https://www.learncpp.com/cpp-tutorial/header-guards/
When I add 'bits/stdc++.h'header file, it shows [Error] reference to 'list' is ambiguous. But when I erase the header and remain all other headers, the code is compiling properly.
#include <bits/stdc++.h>
#include<stdio.h>
#include <iostream>
#include<stdlib.h>
#include <string.h>
#include <sstream>
#define LIST_INIT_SIZE 2
#define NULL_VALUE -99999
#define SUCCESS_VALUE 99999
using namespace std;
int listMaxSize;
int * list;
int length;
void initializeList()
{
listMaxSize = LIST_INIT_SIZE;
list = (int*)malloc(sizeof(int)*listMaxSize) ;
length = 0 ;
}
Thats because the header <bits/stdc++.h> has its own version of list i.e. from the list template <list>.
Having two copies or different definitions of the same would result in ambiguity.
If its working without the bits header (STL) then simply dont use it.
I'm current building an application in which I have a log function that is accessible in most of my classes which was declared as below:
FileHandler.h
#ifndef FILEHANDLER_H
#define FILEHANDLER_H
#pragma once
#include <SDL.h>
#include <string>
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <algorithm>
#include <cctype>
//Include to allow logging
#include "log.h"
class fileHandler
{
public:
fileHandler();
virtual ~fileHandler();
void WriteToFile(const std::string& filename, std::string textToWrite);
std::vector<std::string> ReadFromFile(const std::string& filename);
std::string& TrimString(std::string& stringToTrim);
protected:
private:
class log logHandler;
std::vector<std::string> blockOfText;
std::string currentLine;
};
#endif // FILEHANDLER_H
Log.h
#ifndef LOG_H
#define LOG_H
#pragma once
#include <SDL.h>
#include <string.h>
#include <iostream>
#include <sstream>
#include <fstream>
#include <time.h>
class log
{
public:
log();
virtual ~log();
void static WriteToConsole(std::string textToWrite);
void WriteToLogFile(std::string textToWrite);
protected:
private:
};
#endif // LOG_H
This worked fine for a long time and then I wanted to include another function elsewhere in my application that was only compatible with C++11 so I told the compiler to compile to these standards. I was then receiving an error on "log logHandler" saying log is not a declared name.
I was able to resolve the problem by changing the line to
class log logHandler;
I was wondering if anybody could tell me what has changed between C++03 and C++11 that required me to do this?
EDIT: Included all relevant code to make question more complete.
You don't show your real code (missing ; at the end of the class declaration, no #endif), but chances are that your problem is somehow related to std::log, which has received a new overload in C++11, in combination with a using namespace std somewhere in your code.
Note that the new overload is probably irrelevant to the problem at hand; the real reason may very well be a change somewhere in your compiler's standard-library implementation causing an internal #include <cmath>. This means that even in C++03, your code was only working by sheer coincidence, and a conforming C++03 compiler was always allowed to reject it.
Here is an example program which may reproduce your problem:
#include <cmath>
using namespace std;
struct log
{
};
int main()
{
// log l; // does not compile
struct log l; // compiles
}
Nothing has changed about how the code you posted is treated.
What I suspect is, that you somewhere have an
#include <cmath>
And below that, somewhere else
using namespace std;
This causes your compiler to not be able to unambiguously resolve the name log, since there is std::log (a function) and your class log.
By explicitly stating class log, you tell the compiler that you are referring to the class.
When I include this header File "pathfinding.h":
#pragma once
#include <BWAPI.h>
#include "BWAPI/TilePosition.h"
#include <vector>
#include "PathNode.h"
#include "Logger.h"
#include "ArgosMap.h"
#include "MapField.h"
#include "Utils.h"
#include "ComparePathNodePointer.h"
using namespace BWAPI;
class Pathfinding {
private:
std::vector<PathNode*> openList;
std::vector<PathNode*> closedList;
std::vector<Position*> buildpath(PathNode* targetNode);
void expandNode(PathNode* currentNode, MapField* targetField);
ArgosMap* argosMap;
public:
Pathfinding();
~Pathfinding();
std::vector<Position*> getShortestPath(MapField* startField, MapField* targetField);
};
In this header File "UnitAgent.h":
#pragma once
#include <BWAPI.h>
#include <vector>
#include "ArgosMap.h"
#include "Pathfinding.h"
using namespace BWAPI;
class UnitAgent {
protected:
Unit* unit;
UnitType unitType;
int unitID;
std::vector<Position*> trail;
Position target;
public:
UnitAgent(Unit* unit);
std::vector<Position*> getTrail();
Position getTarget();
Position* getPosition();
int getUnitID();
void setTarget(Position target);
void addPositionToTrail(Position* targetLocation);
void moveTo(TilePosition* targetPosition);
};
I get like a million errors mostly error C2143, C2065. But thats not true, the errors do not exist. When I include the header file in another file its all totally fine (except naturally for the stuff that needs the specific header file).
Any Ideas what I should check. Anyone an Idea how i can check my C++ Code, in a way Eclipse checks my java code. I mean why doesnt Visual Studio do that?
This kind of directive should not be in a header file
using namespace BWAPI;
To start with, why do you need all this
#include <BWAPI.h>
#include "BWAPI/TilePosition.h"
#include <vector>
#include "PathNode.h"
#include "Logger.h"
#include "ArgosMap.h"
#include "MapField.h"
#include "Utils.h"
#include "ComparePathNodePointer.h"
using namespace BWAPI;
in pathfinding.h? Just forward declaring ArgosMap, MapField, PathNode, and Position like
class ArgosMap;
class MapField;
class PathNode;
class Position;
is sufficient for pathfinding.h looking at the declaration of Pathfinding class, the stuff above should go to pathfinding.cpp if it is necessary for implementation of Pathfinding methods. The less stuff and dependencies you have in your headers, the easier it will be to debug.
Declarations in pathfinding.h look fine, problem is that some of the methods are not implemented/are not implemented properly. To find out what this methods are, you would need to narrow the scope of the problem - by removing unnecessary dependencies to start with.
Including pathfinding.h in files that do not use it's methods/methods from other headers included would always work fine...
main.cpp
#include <vector>
#include <iostream>
#include "normal.h"
using namespace std;
int main()
{
return 0;
}
normal.h
#ifndef NORMAL_H
#define NORMAL_H
#include <vector>
#include <iostream>
using namespace std;
vector < int > myvector;
myvector.push_back(12);//does not name a type
#endif
I know I need to somehow include vector<int> myvector in main.cpp but can't figure the way. I've looked at my previous programs and didn't need to include anything in main.cpp.
The problem is that the code
myvector.push_back(12); is not inside any function. Outside of functions you may only declare (and possibly initialize) variables, you cannot put other code.
So, even though you can declare your vector in the .h file (probably to have it available in many files) you should move this line inside the main() or some other function.