C++ undefined reference to function with queue parameter [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 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 )
^^^^^^^^^^

Related

invalid conversion from 'char' to 'const char*' in this simple function [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 3 years ago.
Improve this question
Here is the code:
bool Vehicle::checkID(std::string id)
{
std::vector<int> digits;
for (char c : id)
{
if(std::isdigit(c))
{
digits.push_back(atoi(c));
}
else
{
digits.push_back(int(c));
}
}
I don't know why he throws this error for "digits.push_back(atoi(c))".
I'm a very beginner, I know this will be not that difficult for you.
You can't do:
atoi(c)
atoi() expects a char *. You probably want
digits.push_back(c - '0');
The function atoi() takes a single const char * type as a parameter.
You're calling it with a char parameter. The compiler doesn't know how to convert from one to the other.
atoi actually wants a string as input, you can't call it with a single character. A string always needs a zero character as a terminator.

Const error with this [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 4 years ago.
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.
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.
Improve this question
This is my code:
bool State::operator==(const State& s) const
{
bool flag=true;
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
if(s.GetboardEl(i,j)!=board[i][j]){
flag=false;
}
}
}
return flag;
}
getBoardEl is a function in State class which returns an element from board(int[][]). I get this ERROR even if i'm using const before the brackets({)
:
error: passing 'const State' as 'this' argument of 'int State::GetboardEl(int, int)' discards qualifiers [-fpermissive]|
It appears that the member function getBoardEl is a non-const member function.
You should definitely change that to a const member function since the name implies it is a get function not a set function.
That would resolve the compiler error.
Another way to resolve the error is to use the member variable directly, as you have for this.
if ( s.board[i][j] != this->board[i][j] ) {

C++ error, compiler won't recognize string::push_back [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 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;
}

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>)

sorting a vector of a member inside the struct C++ [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
Hi I would like to sort the members of a vector. I've seen similar problems but the errors faced is different. I follow the "sort" steps at cplusplus.com
struct Food {
char[8] Name;
float Price;
}
And I have a vector
std::vector<Food> FoodList;
And I have a compare function:
bool comparePrice (Food f1, Food f2) { return (f1.Price<f2.Price); }
And final my sort statement:
std::sort(FoodList.begin(),FoodList.end(),comparePrice);
But I am experiencing an error that the sort expects 2 arguments but I gave 3. But when I was writing the program in MVS2010, it prompts me to enter 3 arguments. Can somebody help?
There are several problems with your code, see comments below:
struct Food {
char[8] Name; // this should be: char Name[8];, and even better std::string
float Price;
} // missing semicolon here ;
// You should use const Food& as parameter type
bool comparePrice (Food f1, Food f2) { return (f1.Price<f2.Price); }