int to string error?

Started by
10 comments, last by Kevn 14 years, 10 months ago
I'm getting an error that says error C2664: 'Deque<Item>::pushBack' : cannot convert parameter 1 from 'int' to 'std::string' and error C2664: 'Deque<Item>::pushBack' : cannot convert parameter 1 from 'char' to 'std::string' but I have no clue what is wrong with it... Item is of type string I only posted the piece of my code that the error points to and the place it is used. My program takes a bunch of numbers from a weblog type thing, puts each line into a linked list one at a time, compresses duplicates of a number, and then prints out the numbers. I was using char to do this, which worked fine when compressing for numbers 1-9 but once i reached an area that had multiple 10s or anything above 10 in a row the compression didnt do anything because the char was reading 1 and 0 seperate for 10. So i thought i would just make it string so i can have 10 without it reading 1 and 0 seperate. template <class Item> void Deque<Item>::pushBack(Item item) { if ( head == NULL ){ listStart = true; head = new Node; head->component = item; head->link = NULL; backHolder = head; } else { nodePtr newNodePtr; newNodePtr = new Node; newNodePtr->component = item; if ( listStart == true ){ head->link = newNodePtr; listStart = false; } else { backHolder->link = newNodePtr; } backHolder = newNodePtr; backHolder->link = NULL; } } this is where this function is used: Deque<string> test; string line; ifstream myfile("msnbc990928.seq"); // Opens file if ( myfile.is_open() ){ while ( !myfile.eof() ) // Do till the end of file is reached { getline(myfile, line); // Get a line of the file for ( int count = 0; count < (line.length() - 1); count++ ) { } for ( int count = 0; count < (line.length() - 1); count++ ) { if (line.at(count) != ' '){ if (line.at(count + 1) != ' '){ test.pushBack( (line.at(count) + line.at(count)) ); count++; } else { test.pushBack(line.at(count)); } } } test.compress(); for ( int count = 0; count < (line.length() - 1); count++ ) { test.viewEntry("front"); test.popFront(); } cout << endl; } myfile.close(); }else{ cout << "Unable to open file." << endl; }
Advertisement
std::string doesn't have a constructor taking a single character as an argument. Do you want to make a string containing a single character, or perhaps you want a Deque<char>?
i want to make a string containing one character because it wont always be just one character. lets say one of the numbers is 10, that requires 2 characters( 1 and 0). when i make Deque<char> it works fine untill i get to an area where i need two characters like the number 10. I tried adding two characters together, like 1 and 0 but i get some letter like b instead of 10.
If you wanted to create a string with a single character c you could use std::string(1, c).
if you have one char, you could do something like this:

char a = 'a';char b[2];b[0] = a;b[1] = '\0';std::string string = &b;
i dont just want it to contain 1 character.. I'm saying I want it to contain however many needed. It may be 1 character, it may be 3.
You could create a char array with the characters you want in the array and use that to construct your string, or you could just add single character strings together.
Forget all of the code you have so far, for a moment.

What exactly are you trying to do?
I can't add two character strings together because I can't put it into a character string in the first place. The error i'm getting is because i'm trying to place a value from the at() function of a string ( which returns a single character ) into a string. I was trying to put it into a string to do exactly what u just said, add them together. Trying to do it all with character arrays means im going to have to make a lot of changes and might get kind of confusing.

as for zahlman's comment, i wrote on my first post what the program does but I will make it more clear. Right now I am reading lines of numbers from a file called msnbc990928.seq so that I can have a compressed version of the numbers get printed out to my console. To do this, I am taking a single line at a time from it, sticking that line into a linked list by using a pushBack() function( which takes a character as a parameter) that i made, then using a compress command that i made to get rid of any duplicate numbers that follow eachother, then printing out the linked list and going to the next line. By getting rid of duplicates i mean a line like 5 3 7 7 4 5 8 8 9 would turn out to be 5 3 7 4 5 8 9. Now the trouble i'm running into is that i'm reading each line from the file into a string and then getting the numbers from that string using the at() function. This gives me a character of that number. It all works fine here but when I need a number with multiple digits, a single character doesn't detect that the number, for example 10, is actually 10. It detects it as two seperate numbers, 1 and 0. So when compressing the lines, any number with more than 1 digit doesn't get compressed. I tried to change my pushBack() function to take strings as a parameter but then it gives me an error because the at() function returns characters not strings, which it would be trying to feed the character into the string for the pushback function. I need a way to make it so I can have a number like 10 get recognized as 10 instead of just 1 and 0 so the compression works correctly. If i add the 1 and 0 together in a string, i get something like 'b'.

Here is an example of a failed compression:
1 2 2 3 5 5 10 10 1 2
1 2 3 5 1 0 1 0 1 2
see how the 10 doesn't get compressed.
Quote:Original post by KBakerSR
I can't add two character strings together because I can't put it into a character string in the first place.

Works perfectly fine:
int main(int, char **) {  std::string s = std::string(1, 'm') + std::string(1, 'o') + std::string(1, 'o');  std::cout << s;}

This topic is closed to new replies.

Advertisement