C++ error, compiler won't recognize string::push_back [closed] - c++

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
The function it has an issue with:
string encode (string message, string key) {
string code = "whatever";
string forst;
int num;
string::size_type begin = 0;
message = lower_and_strip(message);
for (char val : message) {
num = return_encoded_char(key, begin, val);
forst = to_string(num);
code.push_back(forst); //*******************************
}
return code;
}
The starred line is what it points to. The return_encoded_char function returns an integer.
The specific error is
proj05.cpp:68:23: error: no matching function for call to 'std::basic_string<char>::push_back(std::string&)' and points to the line I starred.
I initially just declared code without initializing it, but changing that didn't fix it. All the similar questions I could find had some other element to blame; I feel like this should be relatively straightforward, though obviously it's not since it isn't working.
I have #include <stream> and using std::to_string etc. I'm using -std=c++11 to compile it.
Help.
P.S. Using Geany on Linux.

Your code variable is a std::string. The std::string class doesn't have a push_back() method that takes another std::string as input. You should try with the += operator instead, which accepts a character or a string:
string encode (string message, string key) {
string code = "whatever";
string forst;
int num;
string::size_type begin = 0;
message = lower_and_strip(message);
for (char val : message) {
num = return_encoded_char(key, begin, val);
forst = to_string(num);
code += forst; //*******************************
}
return code;
}

Related

Error: Initializing Argument 1 of [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I've looked around and seen quite a few of these, but none of them provided the solution for my problem. I'm getting this compilation error with the following code:
THE ERROR:
THE CODE:
const int TOP_WORDS = 25;
...
void topWords(Hash t, string word, string topA[]);
int main()
{
...
Hash table1;
string word = "example";
string topWordsArr[TOP_WORDS];
table1.addItem(word);
topWords(table1, word, topWordsArr);
...
}
...
void topWords(Hash t, string word, string topA[])
{
int i = 0;
int tempCount = t.itemCount(word);
int tempCount2 = t.itemCount(topA[i]);
while (tempCount > tempCount2 && i < TOP_WORDS) {
i++;
tempCount2 = t.itemCount(topA[i]);
}
if (i > 0)
All the other posts I've seen about this error involved an incorrect syntax with declaring/passing the string array parameter, but I've double and triple checked it all and I'm certain it's correct; though I've been wrong before..
Using my crystal ball:
you're passing the Hash by value
this requires the copy constructor,
you don't have one (or it's botched, private or explicit)
So, take the Hash by reference
void topWords(Hash const& t, std::string const& word, std::string* topA);
Also,
string[] is not a type in C++
don't use using namespace std;
don't use raw arrays; use std::vector<std::string> (or std::array<std::string, N>)

warning C4700: uninitialized local variable 'p' used [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
This code compiles and runs though gives a Microsoft compiler error that I cant fix
warning C4700: uninitialized local variable '' used.
This is in the starting line of the code, I think
void employee::loginemployee()
{
char uname[15];
char pass[15];
char p;
int i=0;
cout<<"\n\t\tEnter User Name :-";
cin>>uname;
puts("\n\t\tEnter Password :-");
while(p!=13)
{
p=_getch();
_putch('*');
pass[i]=p;
i++;
}
pass[i]='\0';
ifstream objdata;
objdata.open("HRStaff",ios::in|ios::out|ios::binary|ios::app);
if(!objdata)
{
cout<<"\n-----Cannot Open the File-----\n";
//return 1;
}
int nflag=0;
while(!objdata.eof())
{
objdata.read((char *)& info, sizeof(info));
if(strcmp(uname,info.uname)==0 )
{
system("cls");
cout<<"\n\n\n\t\t****************************************";
cout<<"\t\t\t\t\t\t\t Welcome TO EMS"<<info.uname<<endl;
cout<<"\t\t****************************************\n"<<endl;
info.putdata("SPS");
cout<<"\n\tPress any key to log out...";
nflag=1;
}
}
if(nflag==0)
{
cout<<"\n\nSorry !! Your Username & Password do not match.";
_getch();
logoutAll();
}
objdata.close();
}
The warning is quite clear. You declare a variable without initialising it:
char p;
then use its uninitialised value:
while(p!=13)
{
// ...
}
Either initialise it before use:
char p = 0; // or any value other than 13
or restructure the logic so its value isn't used until you've assigned to it:
do
{
// ...
} while (p != 13);
Then learn about buffer overflow and stop reading user input into fixed-sized buffers without checking the length. This is C++, not C, so you should usually use std::string to store string values.

Why does my string say (null)? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I'm trying to set filename based on condition but it's not working. It keeps saying (null).
void main()
{
int cond = 1;
char * filename;
// C:\other\path\here\
filename = "C:\\other\\path\\here";
if (cond)
// C:\some\path\here\
filename = "C:\\some\\path\\here";
printf("%s", filename);
}
From the standard,
§2.1.2 [lex.phases]
Each instance of a new-line character and an immediately preceding
backslash character is deleted, splicing physical source lines to form
logical source lines.
So
// C:\other\path\here\
filename = "C:\\other\\path\\here";
becomes
// C:\other\path\here\filename = "C:\\other\\path\\here";
Likewise
// C:\some\path\here\
filename = "C:\\some\\path\\here";
becomes
// C:\some\path\here\filename = "C:\\some\\path\\here";
Hence filename never gets initialized.
The MS C++ compiler gives a warning.
(6) : warning C4010: single-line comment contains line-continuation character
(9) : warning C4010: single-line comment contains line-continuation character
After preprocessing, this will be the code, I think
void main()
{
int cond = 1;
char * filename;
if (cond)
printf("%s", filename);
}
Actually, in the above program filename can have any value, not necessarily null. So your program could print anything, crash, whatever. You are probably compiling debug & your compiler null initializes unitialized pointers in debug mode & hence you get null
Ob: main is always int main not void main

C++ undefined reference to function with queue parameter [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I have created a class that Tokenizes and string input. It converts the components of a string into a queue of strings as follows:
queue<string> Fraction::Tokenize( const string & infixExpression )
{
queue<string> tokens;
string currentToken;
for( char currentChar : infixExpression )
{
currentToken += currentChar;
}
tokens.push(currentToken);
return tokens;
}
This is the function that takes queue as a parameter:
Fraction evaluateInfix( queue<string> & infixQueue )
{
//code goes here
}
However, when I call these functions from a constructor:
Fraction::Fraction( const string &infix )
{
queue<string> myQueue = Tokenize(infix);
*this = evaluateInfix(myQueue);
}
I get the following error:
Fraction.cpp:(.text+0x1fd): undefined reference to `Fraction::evaluateInfix(std::queue < std::string, std::deque < std::string, std::allocator < std::string > > > &)'
and cannot for the life of me figure out why. Thanks for any help.
Your member function definition is missing the class' scope:
Fraction Fraction::evaluateInfix( queue<string> & infixQueue )
^^^^^^^^^^

use of strchr() giving error [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
I am trying to find the occurrence of characters of one string(s1) in other string(s2).
This is part of my code.
for(;i<strlen(s1);i++)
{
int x=strchr(s2,s1[i]);
if(x>0)
count++;
}
But on compiling I get an error pointing to strchr() and says
error: invalid conversion from ‘char*’ to ‘int’ [-fpermissive]
Anyone explain what is problem in using strchr() function.
Assignment is wrong strchr doesn't returns int but returns address of first char in string found:
int x=strchr(s2,s1[i]);
should be:
char* x = strchr(s2, s1[i]);
// ^ returns char*
Read manual
char *strchr(const char *s, int c);
RETURN VALUE
The strchr() and strrchr() functions
return a pointer to the matched character or NULL if the character
is not found. The terminating null byte is considered part of the
string, so that if c is specified as '\0', these functions return a
pointer to the terminator.
And so:
if(x>0)
should be:
if(x != NULL)
or just if(x)