I'm writing a simple class in C++ for a class (school, not code). I have a little C++ experience, but it's been a while so I'm relearning whatever I forgot and learning a lot of new syntax (I have much more experience in Java). Here is the code:
#include <iostream>
#include <string>
using namespace std;
class Project112
{
private:
string romanNumeral;
int decimalForm;
public:
Project112()
{
romanNumeral = "";
decimalForm = 0;
}
int getDecimal()
{
return decimalForm;
}
};
and here is the driver:
include cstdlib
include <iostream>
using namespace std;
int main()
{
Project112 x;
int value2 = x.getDecimal();
return 0;
}
This is part of a larger program, but I've simplified it down to this because this is the where the problem lies. Every time I try to run the program, I get the following errors:
main.cpp:10: error: 'Project112' was not declared in this scope
main.cpp:10: error: expected `;' before 'x'
main.cpp:14: error: 'x' was not declared in this scope
Can someone please explain the problem? Thanks in advance.
#include "Project112.h"
Add that above main. You forgot to include the header file. And:
using namespace std;
Don't do that in a header file. That imports the everything from the std namespace into the global namespace of any file which includes your header. Just fully qualify the type in a header, i.e., std::string, and I would avoid that in implementation files as well in a large project (though something like using std::string is ok IMO in an implementation file).
Related
All I'm trying to do is create a separate class to hold my Hello World function (this is for a class), but I am getting an "identifier is undefined" compiler error. What is the issue?
Here is my main function (helloworld.cpp):
#include <iostream>
using namespace std;
int main() {
print_me();
system("pause");
return 0;
}
And here is the header class (helloworld.h) :
#include <iostream>
void print_me() {
std::cout << "Hello World\n";
}
You have not included helloworld.h in helloworld.cpp. The following code should work:
#include <iostream>
#include "helloworld.h"
using namespace std;
int main() {
print_me();
system("pause");
return 0;
}
One thing to remember is from your compiler's point of view, there is no connection between the two files unless you specify it. The fact that both files have the same name does not have any significance for compiler.
Side note 1: Consider using include guards in your header files. For simple projects, it may not be obviously necessary but for larger projects, not using them can lead to annoying ambiguous compilation errors.
Side note 2: Implementing function bodies in header files is generally discouraged.
To keep class definition in header file clean, I decided to move implementations of templated functions into another *.h file, which I include in the main header. Now I would like to make use of using namespace there, to make code more readable.
But using namespaces would affect the whole application, since the file gets included in the header which itself gets included in the whole application. The using of the namespaces would spread out of the file.
How can I handle this?
You can put using namespace XX inside function definitions to scope the using declaration to that function:
int func( ...args... )
{
using namespace std;
// ... body of function
}
Use a namespace alias.
namespace submod_ = topspace::other_project::module::submodule;
Then you can use submod_ wherever you would require the very long namespace.
This requires you to still use submod_ where you would have the long namespace qualifier. In that sense, it doesn't exactly answer your question. But I would argue that the explicitness of the qualification aids readability and helps prevent mistakes.
There are real examples of StackOverflow questions where a "using" declaration brought in "lurking" functions which the code author did not realize.
You can place the using namespace in the namespace of your 'main header':
Header.h
#include <string>
namespace Test
{
using namespace std;
string f()
{
return "Test";
};
}
main.cpp
#include "Header.h"
int main()
{
Test::f();
string test; // Error: identifier "string" is undefined
std::string test;
return 0;
}
I have several files, listed just below, wherein I define some namespaces and classes. Then in one of the files I am trying to incorporate the classes from the others. The issue is that the compiler seems to not be able to find my namespaces.
charon.cpp
chin.cpp
chout.cpp
main.cpp
My namespaces are charon, charon_in, and charon_out. The main problems come up on a particular file, charon.cpp, so here is that file and chin.cpp too.
The errors:
g++ -c -g -MMD -MP -MF build/Debug/GNU-Linux-x86/sys/charon.o.d -o build/Debug/GNU-Linux-x86/sys/charon.o sys/charon.cpp
sys/charon.cpp:6:17: error: ‘charon_in’ is not a namespace-name
sys/charon.cpp:6:26: error: expected namespace-name before ‘;’ token
sys/charon.cpp:7:17: error: ‘charon_out’ is not a namespace-name
sys/charon.cpp:7:27: error: expected namespace-name before ‘;’ token
sys/charon.cpp:15:5: error: ‘chout_’ does not name a type
sys/charon.cpp:16:5: error: ‘chin_’ does not name a type
sys/charon.cpp:31:39: error: ‘chin_’ has not been declared
sys/charon.cpp:31:55: error: ‘engine_input’ was not declared in this scope
sys/charon.cpp:32:40: error: ‘chout_’ has not been declared
sys/charon.cpp:32:57: error: ‘engine_output’ was not declared in this scope
charon.cpp
#include <iostream>
#include <boost/thread.hpp>
#include <boost/date_time.hpp>
using namespace std;
using namespace charon_in;
using namespace charon_out;
namespace charon
{
class charon_
{
private:
chout_ engine_output;
chin_ engine_input;
boost::thread input_thread;
boost::thread output_thread;
void start_threads();
void stop_threads();
public:
//Methods
};
chin.cpp
#include <iostream>
#include <borland/conio.h>
#include <ncurses.h>
#include <boost/thread.hpp>
using namespace std;
using namespace charon;
using namespace charon_out;
namespace charon_in
{
class chin_
{
private:
bool pause;
charon_* engine;
inline iostream grab();
public:
//Methods
};
I triple checked everything. So I am pretty confident the syntax is correct, but obviously I'm missing some key concept otherwise the compiler wouldn't complain.
(I know putting classes in the cpp files isn't a standard thing to do, but I know it is possible to do it so that's why I chose to try.)
I can't see what mistakes I've made. Any help would be appreciated.
You need to include the header file that declares the namespace (or declare it again) before the using directive:
// using namespace test; // Error test is not known to be a namespace
namespace test {}
using namespace test; // Fine -- test is known
As Wayne correctly points out, you probably want to restructure your code differently, including header files that will contain the declarations and can be included in different translation units.
You have the class declarations and the definitons in the .cpp files. You need to move the class declarations to a .h file and include it in the appropriate files that are using the class.
For example, move the following to chin.h and include chin.h in charon.cpp.
namespace charon_in
{
class chin_
{
private:
bool pause;
iostream key_sequence;
deque<char> key_queue;
charon_* engine;
inline iostream grab();
public:
chin_(const charon_& handle);
chin_(const chin_& orig);
~chin_();
void refresh();
bool stop_check();
};
}
Be wary of cyclic dependencies for they can also cause the compiler to not find the namespace you require.
//In rightWing.h
include "leftWing.h"
// code
//In leftWing.h
include "rightWing.h"
// code
I have a class which is going to handle an array of objects of another class I've created earlier (which works fine). The problem appears when I try to create an object of my List-class.
This is the header of the list-class:
#ifndef personlistH
#define personlistH
#include "Person.h"
#include <iomanip>
#include <iostream>
#define SIZE 10
namespace std {
class PersonList {
private:
Person persons[SIZE];
int arrnum;
string filename;
public:
Personlist();
};
}
#endif
This is the main function:
#include <iostream>
#include "PersonList.h"
using namespace std;
int main() {
PersonList personlist;
return 0;
}
The error my compiler is giving me is the following:
error: "27 \PersonList.h ISO C++ forbids declaration of `Personlist'
with no type"
I've searched for answers but as I'm quite new to C++ it's been a bit confusing and I haven't found any fitting yet. It would be great if you could explain this error for me.
You have the wrong capitalisation on your constructor declaration. You have Personlist(); but need PersonList();. Because what you have isn't equal to the class name it is considered a function rather than a constructor, and a function needs a return type.
Do not add your own types to the standard namespace(std), instead create your own namespace and define your class inside it.
//PersonList.h
namespace PersonNamespace
{
class PersonList
{
//members here
};
}
//Main.cpp
using namespace PersonNamespace;
The actual error is that you made a typo in Personlist instead of PersonList
The error is because you got the capitalisation wrong when you declared the constructor; it should be PersonList() not Personlist().
Also, you should never declare your own classes in the std namespace; that's reserved for the standard library. You shoud make up your own namespace name, and put your things in that.
This has been stumping me for a while. I am trying to create a function that takes a hash table, and returns said hash table. However I am getting this error in the header file,
error: ‘string’ was not declared in this scope.
error: template argument 1 is invalid
Here is the header file itself:
#ifndef NAME_SPAWN_H
#define NAME_SPAWN_H
#include <QString>
#include <QHash>
#include <string>
class Name_Spawn
{
public:
Name_Spawn();
void initalize();
private:
QString int_2_str(int);
void seed();
QHash<string,QString> setTable(QHash<string,QString>);
};
#endif // NAME_SPAWN_H
As you can see, string has been declared. Any ideas? I am at my wits end.
The real name of string is std::string. Try using that instead.
(You can leave off the std:: qualifier only if there's a using namespace std; directive in scope. But it's a good habit not to put using namespace std; in header files.)