Problem with <string> values if used in more than one file

A

Andreas Schmitt

Hi,

I got a problem here that I haven't been able to fix so far since I don't
even know what the problem is.
I have the following two files (among one other 'Class1.cpp') in my Project
in Visual C++
The MainFile.cpp works fine, but as soon as I try to use strings in
Class1.hpp the compiler reports an error
and claims he doesn't know the 'string' variable type.
Why does he know in the first file and not in the second?
I have the necessary <string> included in both files, yet he doesn't know
the string type in one of them..??


Any help would be appreciated
Thanks


********************
** MainFile.cpp:
********************

#include <stdio.h>
#include <string>
#include <time.h>

#include "Class1.hpp"

using namespace std;

int main()
{
....

string myString[3] = {"Content1", "Content2", "Content3"};
....

return 0;
}


********************
** Class1.hpp:
********************

#ifndef CLASS1_HPP
#define CLASS1_HPP

#include <string>

class Class1
{
public:
Team();
~Team();
...

private:
string myString2;
....
};

#endif
 
A

Alipha

********************
** Class1.hpp:
********************

#ifndef CLASS1_HPP
#define CLASS1_HPP

#include <string>

class Class1
{
public:
Team();
~Team();
...

private:
string myString2;
...
};

#endif


it's std::string, not string, and you don't have a using statement.
please just type std::string and don't bother with the using statement.
there's little reason to clutter the global namespace just to save a
few keystrokes. Especially don't have using statements in header files,
since that forces every file which includes that header to also import
those identifiers into the global namespace.
 
A

Andreas Schmitt

Alipha said:
it's std::string, not string, and you don't have a using statement.
please just type std::string and don't bother with the using statement.
there's little reason to clutter the global namespace just to save a
few keystrokes. Especially don't have using statements in header files,
since that forces every file which includes that header to also import
those identifiers into the global namespace.


Thank you.. forgot about the namespace.. thanks
But I seem to have another problem.
Maybe you or somebody else can help me with that one as well..

***************
Class1.hpp
***************

class Class1
{
public:
Class1();
~Class1();
void SetString( std::string String[3] );

private:
std::string myString[3];
};

***************
Class1.cpp
***************

void Class1::SetString( std::string String[3] )
{
myString = String;
}

************************************************

It says now that it cant convert 'std::string[]' to 'std::string[3]'

Did I declare something wrong here?


Thanks again
 
A

Andreas Schmitt

As additional info maybe:

I call the Method from MainFile.cpp like this:

using namespace std;

....

Class1* ExampleObject = new Class1
....

string exampleString[3] = {"Content1", "Content2", "content3"};
....

ExampleObject ->SetString( exampleString );

....
 
X

Xie Yubo

Andreas said:
Hi,

I got a problem here that I haven't been able to fix so far since I don't
even know what the problem is.
I have the following two files (among one other 'Class1.cpp') in my Project
in Visual C++
The MainFile.cpp works fine, but as soon as I try to use strings in
Class1.hpp the compiler reports an error
and claims he doesn't know the 'string' variable type.
Why does he know in the first file and not in the second?
I have the necessary <string> included in both files, yet he doesn't know
the string type in one of them..??


Any help would be appreciated
Thanks


********************
** MainFile.cpp:
********************

#include <stdio.h>
#include <string>
#include <time.h>

#include "Class1.hpp"

using namespace std;

int main()
{
...

string myString[3] = {"Content1", "Content2", "Content3"};
...

return 0;
}


********************
** Class1.hpp:
********************

#ifndef CLASS1_HPP
#define CLASS1_HPP

#include <string>

class Class1
{
public:
Team();
~Team();
...

private:
string myString2;
...
};

#endif
Look, in Class1.hpp, you just only include the <string> but don't
declare the "std" namespace. You need to add the follow line:
using namespace std;

--
Best Regards

Xie Yubo
Email: (e-mail address removed) Website: http://xieyubo.cn/
Harbin Institute of Technology
Phone: 86-451-86416614 Fax: 86-451-86413309
 
R

Ram

std::string myString[3];
};

***************
Class1.cpp
***************

void Class1::SetString( std::string String[3] )
{
myString = String;
}

************************************************

It says now that it cant convert 'std::string[]' to 'std::string[3]'

Did I declare something wrong here?

I presume you intent to copy String contents to myString. Contents of
array (or pointer) variables can't be copied that way. Also array
variables are kind of const, they can't be reassigned. Use
void Class1::SetString( std::string String[3] )
{
for(int i=0; i<3; ++i)
myString = String;
}

Ramashish
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top