| Topic: |
DEVELOP > c-Plus-Plus |
| User: |
"Andreas Schmitt" |
| Date: |
31 Jul 2005 01:08:30 PM |
| Object: |
Problem with values if used in more than one file |
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
.
|
|
| User: "Alipha" |
|
| Title: Re: Problem with values if used in more than one file |
31 Jul 2005 01:40:39 PM |
|
|
********************
** 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.
.
|
|
|
| User: "Andreas Schmitt" |
|
| Title: Re: Problem with values if used in more than one file |
31 Jul 2005 02:02:23 PM |
|
|
"Alipha" <aliphax@hotmail.com> schrieb im Newsbeitrag
news:1122835239.806417.202010@g14g2000cwa.googlegroups.com...
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
.
|
|
|
| User: "Ram" |
|
| Title: Re: Problem with values if used in more than one file |
01 Aug 2005 03:20:10 PM |
|
|
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[i] = String[i];
}
Ramashish
.
|
|
|
|
| User: "Shezan Baig" |
|
| Title: Re: Problem with values if used in more than one file |
01 Aug 2005 03:50:24 PM |
|
|
Andreas Schmitt wrote:
std::string myString[3];
Use "std::vector<std::string> myString;"
-shez-
.
|
|
|
|
|
| User: "Andreas Schmitt" |
|
| Title: Re: Problem with values if used in more than one file |
31 Jul 2005 02:24:42 PM |
|
|
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 );
....
.
|
|
|
|
|
| User: "Xie Yubo" |
|
| Title: Re: Problem with values if used in more than one file |
31 Jul 2005 06:50:08 PM |
|
|
Andreas Schmitt wrote:
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: Website: http://xieyubo.cn/
Harbin Institute of Technology
Phone: 86-451-86416614 Fax: 86-451-86413309
.
|
|
|
|

|
Related Articles |
|
|