473,386 Members | 1,790 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Problem with <string> 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
Jul 31 '05 #1
6 1299
> ********************
** 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.

Jul 31 '05 #2

"Alipha" <al*****@hotmail.com> schrieb im Newsbeitrag
news:11**********************@g14g2000cwa.googlegr oups.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
Jul 31 '05 #3
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 );

....
Jul 31 '05 #4
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: xi*****@gmail.com Website: http://xieyubo.cn/
Harbin Institute of Technology
Phone: 86-451-86416614 Fax: 86-451-86413309
Jul 31 '05 #5
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[i] = String[i];
}

Ramashish

Aug 1 '05 #6

Andreas Schmitt wrote:
std::string myString[3];

Use "std::vector<std::string> myString;"

-shez-

Aug 1 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

37
by: Zombie | last post by:
Hi, what is the correct way of converting contents of a <string> to lowercase? There are no methods of <string> class to do this so I fallback on strlwr(). But the c_str() method returns a const...
6
by: Karl Ebener | last post by:
Hi! I am currently using a string to hold data (can be strings as well as binary!). Now it occured to me, that the <string> might be unable to handle Null-Bytes. Is that so? Following...
6
by: buzzweetman | last post by:
Many times I have a Dictionary<string, SomeTypeand need to get the list of keys out of it as a List<string>, to pass to a another method that expects a List<string>. I often do the following: ...
5
by: Etrex | last post by:
Hello, This is my first attempt at a c++ program, and it is a long post, please bear with me. I'm trying to read in a text file containing a firewall log, make the information...
6
by: arnuld | last post by:
This works fine, I welcome any views/advices/coding-practices :) /* C++ Primer - 4/e * * Exercise 8.9 * STATEMENT: * write a program to store each line from a file into a *...
4
by: parez | last post by:
Hi, I am trying to serialize List<List<string>. With the following code public List<List<string>DataRows { get; set; }
6
by: Mr. K.V.B.L. | last post by:
I want to start a map with keys but an empty vector<string>. Not sure what the syntax is here. Something like: map<string, vector<string MapVector; MapVector.insert(make_pair("string1",...
42
by: barcaroller | last post by:
In the boost::program_options tutorial, the author included the following code: cout << "Input files are: " << vm.as< vector<string() << "\n"; Basically, he is trying to print a vector...
9
by: barcaroller | last post by:
1. If I pass pointers (char*) as iterators to an STL algorithm and the return value is an iterator, can I convert that iterator to a pointer? If yes, how? 2. What is the internal...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.