I have a my.h file:
#ifndef __MY__
#define __MY__
#include <string>
#include <time.h>
class S
{
public: S();
std::string myname;
};
#endif
my.cpp
#include "my.h";
#include<string>
#include<iostream>
using namespace std;
S::S()
{
// .. code
}
I want to create an so file. There is no error when creating it. But when I compile the .h file it says: string:No such file or directory. If I pus string.h instead of string I have the error: expected '=',',',';','asm', before S (at class S) in my.h.
In the .cpp file (if i change the string with string.h) i have after i compile error: string in namespace std does not name a type. WHERE AM I WRONG?
Well, first, it seems that you come from java because when you typed:
class S
{
public: S();
std::string myname;
};
I guess you actually meant:
class S
{
public:
S();
private:
std::string myname;
};
In the .cpp file, you typed s instead of S: note that C++ is case-sensitive regarding classes names.
Also, regarding your problem, I suspect you are currently using a C compiler and not a C++ compiler. Without knowing the used command-line, I can't say much more on that.
Try this
#ifndef MY_H
#define MY_H
#include <string>
#include <time.h>
class S
{
public: S();
std::string myname;
};
#endif
#include "my.h"
#include<string>
#include<iostream>
using namespace std;
S::S()
{
//code
}
Related
Folks,
I crated a name space in a single file, but how exactly do I separate the definition and declaration?
Can I create a .h for declaration and a .cpp for definition just like in a class set of file? If yes, then, do I need to include the namespace header file in my new program, or is the using namspace mynamespace enough?
Any decent C++ book should cover namespaces and code separation.
Try something like this:
myfile.h
#ifndef myfile_H
#define myfile_H
namespace myns
{
class myclass
{
public:
void doSomething();
};
}
#endif
myfile.cpp:
#include "myfile.h"
void myns::myclass::doSomething()
{
//...
}
And then you can use it like this:
#include "myfile.h"
myns::myclass c;
c.doSomething();
Or:
#include "myfile.h"
using namespace myns;
myclass c;
c.doSomething();
I made a class. the header file is :
#pragma once
#include <string>
class Player
{
public:
Player();
private:
};
and the cpp file is :
#include "Player.h"
#include <iostream>
Player::Player()
{
}
When I define a string in the header file and add an argument to the Player function in the header file everything works fine
#pragma once
#include <string>
class Player
{
public:
Player(string name);
private:
string _name;
};
but when I add the same argument to the Player function in the cpp file
#include "Player.h"
#include <iostream>
Player::Player(string name)
{
}
I get an error: identifier "string" is undefined and I get the same error in the header file as well so it effects that too. I tried including string in the cpp file in hopes of solving the problem but it did not work. I'm really desperate for a solution, guys.
All STL types, algorithms etc are declared inside std namespace.
To make your code compile, string type should also specify the namespace as:
Player(std::string name); /* Most recommended */
or
using namespace std;
Player(string name); /* Least recommended, as it will pollute the available symbols */
or
using std::string;
Player(string name);
I have an Object base class, and I have several derived classes called Item, Person, and Location.
Because each of these are derived from Object I need to include Object.h in each of their header files, and I include all of the derived classes in my main.
Because I am doing that I am getting a redefinition error.
What I want to know is what is the correct way to include these files to avoid this error?
Thanks!
EDIT:
object.h
using namespace std;
class Object{
string name;
string description;
public:
Object();
Object(string name, string description);
void set_name(string name);
void set_description(string description);
string get_name();
string get_description();
~Object();
};
item.h
using namespace std;
#include "object.h"
class Item : public Object{
public:
Item();
Item(string name, string description);
};
locale.h
using namespace std;
#include "object.h"
class Locale : public Object{
public:
Locale();
Locale(string name, string description);
};
main.cpp
#include <iostream>
#include <string>
#include "locale.h"
#include "item.h"
using namespace std;
int main(){
return 0;
}
Strange, everybody I've met that hits this problem does not have a slightest idea what is going on and you have properly analysed the problem.
Read this:
http://en.wikipedia.org/wiki/Include_guard
You should add include guards to your headers. This prevents headers from being included twice. For example, at the the top of the Object.h header, you would put,
#ifndef _OBJECT_H
#define _OBJECT_H
and then you end the header with,
#endif
If the header has already been included, the text between #ifndef and #endif is dropped.
If you haven't got them in place already, you need to put include guards into you header files to prevent including the same files multiple times (which would redefine the classes).
I have a file GetL.hxx
#ifndef GetL_included
#define GetL_included
#include <iostream>
using namespace std;
class GetL
{
public:
virtual int getWidth();
};
#endif //GetL_include
Here the class GetL contains only one virtual function. what should i put in source file i.e. in GetL.cxx
#include "GetL.hxx"
int GetL::getWidth() {
// your code goes here
}
By the way, having using namespace std; in a header file is not a good practice.
guys i am new to c++
i am trying to create an class these are my files
//main.cpp
#include <iostream>
#include "testing/test.h"
#include <string>
using namespace std;
int main(void)
{
test c;
c.set_url("e");
}
test.h
#ifndef TEST_H_
#define TEST_H_
#include<string>
class test {
public:
void testing(string url);
};
#endif /* TEST_H_ */
//test.cpp
#include <iostream>
#include<string>
using namespace std;
void crawl::testing (string url) {
cout<< "i am from test class";
}
i am getting error: ‘string’ has not been declared error
The problem is you need to use the fully qualified name of string since the std namespace is not imported
class test {
public:
void testing(std::string url);
};
Please avoid the temptation to fix this by using the std namespace within the testing.h file. This is generally speaking bad practice as it can change the way names are resolved. It's much safer, albeit a bit annoying, to qualify names in header files.
The error you are getting is from not using the namespace std in the header file ie. std::string and not having the using namespace std; before your inclusion of the header file (or in the header).
The command using namespace std; is saying assume a class may be in this namespace, but it only applies to all the uses after the command.
If you did this instead it would also work, though in general this is bad form.
#include <string>
using namespace std;
#include "testing/test.h"
Also, don't forget to include test.h into test.cpp.