working on a college assignment, we're required to write around a provided main.cpp file, giving a translator.h and translator.cpp file. This code compiles and works if I combine it all together into a single .cpp file, but as separate files, it looks as if the header file isn't being recognized, and the compiler throws a load of
I tried using namespace std in the header file, I realize I shouldn't use namespace std as it's bad practice, but it didn't make a difference anyway.
Much appreciated.
These are the errors (continuing in this fashion all the way down)
Translator.cpp:1:1: error: 'Translator' does not name a type
Translator::Translator(const char dictFileName[]) : dictionary(dictFileName)
^
Translator.cpp:5:6: error: 'Translator' has not been declared
void Translator::toElvish(char * outputline, const std::string inputline)
^
Translator.cpp:5:52: error: 'string' in namespace 'std' does not name a type
void Translator::toElvish(char * outputline, const std::string inputline)
^
The code is linked http://pastebin.com/Nwh7mh6D (I think it's probably a bit long for the body of a post like this.
Thanks again.
The problem is that your translator.cpp doesn't #include "translator.h". It also needs to be able to see the class definition and member function declarations. The compiler doesn't care that the two files both happened to be named in the same way and so doesn't automatically connect them together.
Related
I am modifying a large project written in C++.
In one particular cpp file I write
std::vector<CameraObjectPtr> element;
and when I do make I got :
In member function .... error: ‘CameraObjectPtr’ was not declared in this scope
std::vector<CameraObjectPtr> element;
Now, CameraObjectPtr is a type defined as
typedef std::shared_ptr<CameraObject> CameraObjectPtr;
in a file called object.hpp.
The thing is, I am including this file in the original file (the one with the error) by putting in the top
#include "base/type/object.hpp"
I can't figure it out why it is not finding the type. What can be lacking? Some edition of a make file perhaps?
EDIT:
I figured it out what was the problem.
No matter what you include, always be careful about types defined in a different namespace
I modified it to
std::vector<anotherNamespace::CameraObjectPtr> element;
and now it compiles.
Below are two files, one contains the main functions and the other the h file
main
class issuesofRelevance{
public:
vector<string> issues;
int significance = 0; //initalize 0
int approach = 0; //initalize 0
issuesofRelevance() {
}
void setStance(vector<string> issues, int significance, int approach){
std::random_device random_device;
std::default_random_engine generator(random_device());
std::normal_distribution<double> distribution(0,1);
}
};
h file
class issuesofRelevance
{
public:
std::vector<std::string> issues;
int significance;
int approach;
issuesofRelevance(){}
public:
void setStance(std::vector<std::string> issues, int significance, int approach){}
};
When i attempt to compile these 2 files i get the error
error: redefinition of ‘class issuesofRelevance’
class issuesofRelevance{
Im unsure as to why this is occuring
Let's say the main function is in main.cpp and the class is in IssuesofRelevance.h. You didn't mention anything about if you included the .h file in your .cpp file. But I am assuming that you did. So I am assuming you wrote
#include "IssuesofRelevance.h"
At the top of the main.cpp function. Now, what #include directive does, it takes the content of the included file, and copy pastes it in to the main.cpp file. Now you see, you have a class declaration in IssuesofRelevance.h file which is copy pasted in the main.cpp. Also, you have the same declaration in main.cpp file. So the compiler is seeing that you are declaring two classes having same names. Which is not allowed.
So if the two classes are the same then cut the one in main.cpp to the IssuesofRelevance.h and just go with including the .h file in the main.cpp. Everything should be fine.
Though in this way it will work. But, you might want a separate .h file and .cpp file for your class. The .h file contains the declaration. Declaration means the class' member variable and method signatures. And .cpp file will contain the method bodies. I think you wrote the definition in main.cpp file. You can do the declaration in the following way.
issuesofRelevance.h
class issuesofRelevance
{
public:
std::vector<std::string> issues;
int significance;
int approach;
issuesofRelevance();
public:
void setStance(std::vector<std::string> issues, int significance, int approach);
};
issuesOfRelevance.cpp
#include "issuesofRelevance.h"
issuesofRelevance::issuesofRelevance(){
}
void issuesofRelevance :: setStance(std::vector<std::string> issues, int significance, int approach){
std::random_device random_device;
std::default_random_engine generator(random_device());
std::normal_distribution<double> distribution(0,1);
}
Put your class along with relevant code you desire in a header file (.h extension, and easily configurable in workspace by an editor such as Visual Studio), and call it as in when required, using #include"filename.h".
Note that header files are basically chunks of code copy-pasted into the source file and so if they include different definitions of the same function or say class for your case, it will result in ambiguity and throw an error - which is the situation in this scenario for your class issueofRelevance.
Corresponding compiling & linking errors:
If you write two same class definitions together in a file, then it will throw up an error due to the arising ambiguity which is detected by the compiler, since its only in a single file.
But when you place those two same definitions in different files and import them into your cpp file twice (or say you import them in another cpp file and build the solution like in visual studio) it will throw up a linker error as the linker is involved (multiple files - wherein the job of the linker is to link them into a combined executable) and it cannot select multiple definitions present in different files.
If your dealing with functions, then a solution to resolve that is to make the functions static, so that they are defined internally or only for the file they are being compiled against. This makes it possible to have multiple function definitions of the same function in different files with no linking error. Another option is to make it in-line.
You have defined the class issuesofRelevance twice so you got the compile error. You'd better define the class in one header file, and include it from other CPP file.
I have fixed your code and it's host online:
https://www.onlinegdb.com/ByuyF7BsL
It seems that you are new to c++, it's recommended to read some books like c++ primer
I have two header files that I'm including from my main.cpp that have the following definition in an anonymous namespace: const string strToken = ("%");
Compiling using g++ version 4.9 results in this:
In file included from main.cpp:25:0:
libraries/trace.h:31:14: error: redefinition of ‘const string {anonymous}::strToken’
const string strToken = ("%");
^
In file included from libraries/debuglogger.h:12:0,
from libraries/packet.h:10,
from main.cpp:20:
libraries/strformat.h:23:14: note: ‘const string {anonymous}::strToken’ previously declared here
const string strToken = ("%");
^
I had thought that putting something in an anonymous namespace limited it to file scope, making this a non-issue. Am I missing something, or is this some sort of defect in GCC? If anyone wants the full code I would be willing to include it, but I'm hoping I've included enough information in my question to not need it.
I had thought that putting something in an anonymous namespace limited it to file scope
You were just a bit off. Putting things in an anonymous namespace limits their visibility to the translation unit they appear in. A translation unit is everything that turns into a single object file, which is typically one source file (.cpp file) along with all the code from the header files that are included from that source file. So, when you have main.cpp which includes two headers that have the same declarations (in an anonymous namespace or not), you get an error.
Always think about the #include statements as being essentially just a copy-paste of the content of the header in place of the include statement. These kinds of issues become a lot easier to deal with once you understand that. For example, your particular case basically boils down to having this code after the include-statement have been resolved (copy-pasted into the source file):
// from first header:
namespace {
const string strToken = ("%");
};
// from second header:
namespace {
const string strToken = ("%");
};
int main() { ... };
where the error is pretty obvious, and anonymous namespace really won't change anything to the basic problem that you have multiple definitions of the same thing.
Anonymous namespaces are mainly there so that you can create things inside a translation unit (usually in the .cpp file itself) without having any traces of them appearing after compilation of the translation unit into an object file. You might find this tutorial helpful in understanding the whole compilation and linking process.
I've discovered after doing some research that anonymous namespaces in headers are a bad idea. See here for possible solutions: Hiding a C++ class in a header without using the unnamed namespace
I know that include is for classes and using is for some built-in stuff, like namespace std... When you include something, you can create objects and play around with them, but when you are "using" something, then you can use some sort of built-in functions. But then how am I supposed to create my own "library" which I could "use"?
Simply put #include tells the pre-compiler to simply copy and paste contents of the header file being included to the current translation unit. It is evaluated by the pre-compiler.
While using directive directs the compiler to bring the symbol names from another scope in to current scope. This is essentially put in effect by the compiler.
But then how am I supposed to create my own "library" which I could "use"?
Namespaces are something which are used for preventing symbol name clashes. And usually every library implementer will have their functionality wrapped up in one or many namespaces.
'include' basically does a copy-paste the value of a file to the location of the "include" line. This is used to make your source code (usually .c file) aware of a declaration of other source code (usually sits in .h file).
'using' basically tells the compiler that in the next code you are using something (usually a namespace) so you won't have to do it explicitly each time:
Instead of:
std::string a;
std::string b;
std::string c;
You could write:
using namespace std;
string a;
string b;
string c;
You can say both gives the same functionality but #include is not done by compiler where as using is done by compiler. In #include all the code is placed in file where #include is given where as namespace gives the definition of function and variables from one scope to another. If you have function with same name in two header files and both are included then there will be an error of redeclaration but you can use same name functions if they are from different namespaces.
I am trying to compile this code, and g++ keeps telling me that 'TimeFinder' has not been declared
Header File
#ifndef _TIMEFINDER_H
#define _TIMEFINDER_H
#include <vector>
#include "timefinder.cpp"
using namespace std;
class TimeFinder
{
public:
static vector<int> time_from_name(string filename);
static int calc_seconds (vector <int> time);
};
#endif
CPP File
#include "timefinder.h"
using namespace std;
vector<int> TimeFinder::time_from_name(string filename)//Line 14
{
//Method Body
}
int TimeFinder::calc_seconds (vector <int> time1)//Line 37
{
//Method Body
}
Why is this going on? I looked at other examples online, and my code seems to match what works for other people...
Edit: The exact error messages are
timefinder.cpp:14: error: ‘TimeFinder’ has not been declared
timefinder.cpp:37: error: ‘TimeFinder’ has not been declared
Edit2: I'm sorry I'm not very good at this yet, but I would like to thank everyone for their suggestions. Hopefully my code quality will begin to improve because of them.
Do not do this:
#include "timefinder.cpp"
You are pulling your definitions into your header so they appear before their declarations.
There is a lot of other stuff wrong with your code - the use of static members in the first place, passing vectors and strings by value when they should be references, and placing using directives in header files, but removing that #include should fix the immediate problem.
#include "timefinder.cpp"
Header files usually don't include their .cpp implementation file but only whatever other .h file is required to satisfy the definitions required. Usually it's the other way around where .cpp files include the required .h files necessary to fulfill the dependencies.
using namespace std;
This can also cause a lot of havoc and should be avoided in header files. In header files you should always fully qualify your names with their namespace. The reason is if you import this header file into another one that defines the same symbol locally in their namespace like "cout" then both the std::cout and user::cout would clash and cause a compile error or even worse possibly pick the wrong definition and cause a linking error later.