string and integer combined

B

Bernhard Hidding

Hello,
I want to combine a string with an integer. I work on SuSE 9.1 with g++. Why
does this piece of code not work?

#include <iostream.h>
#include <stdio.h>
#include <fstream.h>
#include <string>
....
intvar intg = 0;
std::string arraystring = "array" + intvar +".dat";
....

g++ gives the following error message:

error: invalid operands of types `const char*' and `const char[5]' to binary
`operator+'

Thanks for your help,

Bernhard
 
R

Rolf Magnus

Bernhard said:
Hello,
I want to combine a string with an integer. I work on SuSE 9.1 with
g++. Why does this piece of code not work?

#include <iostream.h>

#include said:
#include <stdio.h>
#include <fstream.h>

#include said:
#include <string>
...
intvar intg = 0;
std::string arraystring = "array" + intvar +".dat";
...

g++ gives the following error message:

error: invalid operands of types `const char*' and `const char[5]' to
binary `operator+'

"array" is of type array of char. When used, it "decays" into a pointer
to char. If you add an int to a pointer, the result is another pointer,
with the int value as offset. So if intvar had the value 3, the
resulting pointer would point to the second 'a' of "array". Then, you
try to add ".dat" to it, which also decays into a pointer. And you
can't add two pointers together. Try this instead:

std::stringstream stream;
stream << "array" << intvar << ".dat";
std::string arraystring = stream.str();
 
B

Bernhard Hidding

std::stringstream stream;
stream << "array" << intvar << ".dat";
std::string arraystring = stream.str();

This does not work: g++ says

"aggregate `std::stringstream stream' has incomplete type and cannot be
defined "

Bernhard
 
V

Victor Bazarov

Bernhard said:
I want to combine a string with an integer. I work on SuSE 9.1 with g++. Why
does this piece of code not work?

Because it's not standard C++.
#include <iostream.h>
#include <stdio.h>
#include <fstream.h>
#include <string>

I am not even going to say anything about .h headers here.
...
intvar intg = 0;
std::string arraystring = "array" + intvar +".dat";
...

g++ gives the following error message:

error: invalid operands of types `const char*' and `const char[5]' to binary
`operator+'

What effect are you looking for? When you're done figuring that
out, explain me how it would work in this case (and what the output
should be):

#include <string>
#include <iostream>

int main() {
int index[] = { 0, 5, 10 };
const char keywords[] = "HELO\0OPEN\0EXIT\0";
std::string helostr(keywords + index[0]);
std::string openstr(keywords + index[1]);
std::string exitstr(keywords + index[2]);

std::cout << helostr << ' ' << openstr << ' ' << exitstr << '\n';
}

Victor
 
R

Rolf Magnus

Bernhard said:
This does not work: g++ says

"aggregate `std::stringstream stream' has incomplete type and cannot
be defined "


you need to #include <sstream>
 
T

Thomas Matthews

Bernhard said:
Hello,
I want to combine a string with an integer. I work on SuSE 9.1 with g++. Why
does this piece of code not work?

#include <iostream.h>
#include <stdio.h>
#include <fstream.h>
#include <string>
...
intvar intg = 0;
std::string arraystring = "array" + intvar +".dat";
...

g++ gives the following error message:

error: invalid operands of types `const char*' and `const char[5]' to binary
`operator+'

Thanks for your help,

Bernhard

Because of precedence rules for addition of arrays and pointers. Also
because the string class has no constructors for converting from int to
string nor for appending an integer quantity.

In a string concatenation situation, the operator '+' is overloaded to
perform concatenation:
array_string = "array" + " hello";
which is equal to:
array_string = string("array").operator+(string(" hello"));

Applying the above to a string concatenated with an integer would
look like:
array_string = "array" + 4; /* numeric const or int variable */
array_string = string("array").operator+(4);
In order to accomplish what you want, the string class would
need a method of:
string& string::eek:perator+(int);
which doesn't exist.

However, some non-standard string libraries have constructors
that can take integral arguments and convert them to strings.
So, assuming a specialized string class called AnsiString that
has such an operator, you would want:
AnsiString arraystring;
arraystring = "array" + AnsiString(intvar) + ".dat";

Look up istringstream and ostringstream. Another alternative
is to use sprintf.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
 

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

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top