I keep getting the following error: error: no matching function for call to 'Stack::Peek()'
I am new to C++ and I cannot figure out why I am getting the error
This is my Peek() function.
int Peek(T data)
{
if(IsEmpty ())
return -1;
else
return top -> data;
}
and this is my main() function.
int main()
{
Stack<int> s1;
cout << "*declare stack s1\ns1=" << s1 << endl; // stack initially set to 0
cout << "s1.Size()=" << s1.Size() << endl;
cout << "s1.IsEmpty()=" << ((s1.IsEmpty()) ? "T" : "F") << endl;
cout << "s1.IsFull()=" << ((s1.IsFull()) ? "T" : "F") << endl;
cout << "s1.Peek()=" << s1.Peek() << endl;
cout << endl;
Stack<char> s4;
for (char c='a'; c<='z'; c++)s4.Push(c);
cout << "s4=" << s4 << endl;
cout << "s4.Size()=" << s4.Size() << endl;
cout << "s4.IsEmpty()=" << ((s4.IsEmpty()) ? "T" : "F") << endl;
cout << "s4.IsFull()=" << ((s4.IsFull()) ? "T" : "F") << endl;
cout << "s4.Peek()=" << s4.Peek() << endl;
}
I get the error whenever the Peek function is called in main so I was wondering if anyone could help me with this.
You defined a Peek method, but not Stack::Peek. Your method should have this signature: int Stack::Peek(T data)
Because you are calling it without any argument. Your declaration:
int Peek(T data)
Your invocation:
s4.Peek()
Indeed a stack data type doesn't need an argument to the peek function (nor you use it). You should modify your original function to int Peek().
Your function Peak is declared as having one parameter
int Peek(T data);
However you call it without any argument
cout << "s1.Peek()=" << s1.Peek() << endl;
So the compiler does not know a function with name Peak that has no parameter.
Related
using namespace std;
class map
{
private:
float result= 0;
public:
void func_1();
void setres(float counter);
float getres();
};
void map::setres(float counter)
{
result= counter;
}
float map::getres()
{
return result;
}
void map::func_1()
{
float num_0=0, num_1=0, sum=0, i;
map run;
cout << run.getres() << " Result." << endl;
if(result != 0)
cout << "We already have a result saved and it's: " << run.getres() << endl;
else
{
cout << "Give me first number: ", cin >> num_0;
cout << "Give me second number: ", cin >> num_1;
sum= num_0+num_1;
cout << "Result is: " << sum << endl;
}
cout << "The program will save the result." << endl;
run.setres(sum);
cout << "The saved result is: " << run.getres() << "\nPress 1 to repeat the function and check\nif the result is saved." << endl;
cin >> i;
if(i==1)
run.func_1();
}
int main()
{
map go;
go.func_1();
return 0;
}
I don't know why the private variable result is not saved. And how can i make it work.
Then i start compiling it works fine, private result is changing, but then i reopen the function, the result is back to 0 and i wanted it ro be the last result.
Example:
I put 4
I put 7
Sum is 11
And saved result is 11
Then i press 1 to go to the start the result is 0 again, but i wanted it to be 11 not 0.
Within the function you are creating a local variable of the type map
map run;
the data member result of which is changed. That is the function does not change the data member result of the object for which the function is called.
Moreover for example in this code snippet
cout << run.getres() << " Result." << endl;
if(result != 0)
you are accessing the data member result of two different object. In the first statement
cout << run.getres() << " Result." << endl;
you are accessing the data member of the local object run while in the next statement
if(result != 0)
you are accessing the data member result of the object (the object go declared in main) for which the member function is called.
So remove the declaration in the function
map run;
and instead of expressions like for example run.getres() use either just getres() or this->getres().
The issue is that your function is not using members of the object the method is called on. Instead you create a new instance inside the function:
void map::func_1()
{
float num_0=0, num_1=0, sum=0, i;
map run; // <---------- here
//...
Thats why every time you call the function you get a new fresh object. You do not need to create that instance. You already create one in main and inside the member functions you can access its members. As a fix you can remove all run. from the code. Eg
cout << run.getres() << " Result." << endl;
->
cout << getres() << " Result." << endl;
or if you prefer
cout << this->getres() << " Result." << endl;
The value is saved to run, and run.func_1() is called for checking, but then run.getres() is called there. This run is new map object and differ from the run in which the data is saved.
You checked result != 0, but you used run.getres() to print the result. Inconsisitency here.
Instead of
cout << run.getres() << " Result." << endl;
if(result != 0)
cout << "We already have a result saved and it's: " << run.getres() << endl;
You should do
cout << result << " Result." << endl;
if(result != 0)
cout << "We already have a result saved and it's: " << result << endl;
or
cout << getres() << " Result." << endl;
if(getres() != 0)
cout << "We already have a result saved and it's: " << getres() << endl;
I have this function to solve the Tower of Hanoi problem and fortunately It's working good but can anybody explain to me if the function is calling it self before the cout statement in case m!=0 then how does it ever reach the cout statement or even the other call of itself ??
#include <iostream>
using namespace std;
void Hanoi(int m, char a, char b, char c){
if(m == 1){
cout << "Move disc " << m << " from " << a << " to " << c << endl;
}else{
Hanoi(m-1, a,c,b);
cout << "Move disc " << m << " from " << a << " to " << c << endl;
Hanoi(m-1,b,a,c);
}
}
int main(){
int discs;
cout << "Enter the number of discs: " << endl;
cin >> discs;
Hanoi(discs, 'A', 'B', 'C');
return 0;
}
Calling Hanoi(m), where m > 1: First it executes Hanoi(m-1) and all resulting calls. Then it executes cout. Then it executes Hanoi(m-1) and all resulting calls a second time.
Consider m == 3:
Hanoi(3)
Hanoi(2)
Hanoi(1)
cout
cout
Hanoi(1)
cout
cout
Hanoi(2)
Hanoi(1)
cout
cout
Hanoi(1)
cout
I am getting this error when I compile my program, but I don't see anything wrong with the code.
Error 1 error C3867: 'President::getFirstName': function call missing argument list; use '&President::getFirstName' to create a pointer to member h:\president_folder\president_folder\president_driver.cpp 95 1 president_folder
I'm not sure how to interpret it.
//outputData function
void outputData(President prez_array[],fstream &outFile, int count)
{
for(int i = 0; i < count; i++)
{
outFile << prez_array[i].getFirstName << endl;
outFile << prez_array[i].getLastName << endl;
outFile << prez_array[i].getBeginYear << endl;
outFile << prez_array[i].getEndYear << endl;
outFile << prez_array[i].getPartyAffil << endl;
outFile << endl;
}
}
it looks that you forget how to call a function:
outFile << prez_array[i].getFirstName() << endl;
outFile << prez_array[i].getLastName() << endl;
outFile << prez_array[i].getBeginYear() << endl;
outFile << prez_array[i].getEndYear() << endl;
outFile << prez_array[i].getPartyAffil() << endl;
I need help getting declared string function to change white space of input file to a specific character.
if (infile.fail())
{
cout << "The file doesn't exist";
exit(-1);
}
else
{
numBooks = readFile (infile, magSub, 260);
for (i=0; i<numBooks; i++)
{
cout << "Last Name: " << magSub[i].lastName << endl;
cout << "First Name: " << magSub[i].firstName << endl;
cout << "Street Address: " << magSub[i].address << endl;
cout << "City: " << magSub[i].city << endl;
cout << "State or Province: " << magSub[i].state << endl;
cout << "Country: " << magSub[i].country << endl << endl;
cout << "Zip or Postal Code: " << magSub[i].zip << endl;
cout << "Expiration Date: " << magSub[i].expDate << endl;
cout << "Subscriber Number: " << magSub[i].subNum << endl << endl;
}
writeFile(outfile, magSub, numBooks);
}
}
void fillSpace (string &expDate)
{
for (int i=0; expDate.length(); i++)
{
if (isspace(expDate[i]))
expDate[i] = '0';
}
}
I have the function declared above main. I know I need to call the function but I can't get it to change the white spaces.
In your code for fillSpace, you are not checking for the end of string condition. You should use i<expDate.length() for checking the end of string.
You have missed the check condition in for loop of fillSpace function.
for (int i=0; i < expDate.length(); i++)
And for calling the function
you have to declare a string which will store the string from the magSub[i].expDate.
and then pass that string to the function fillSpace.
After that you will get the string with replaced char space with '0'.
cout << "Expiration Date: " << magSub[i].expDate << endl;
please use the following code:
string temp = magSub[i].expDate; // copy the string to the temp string/char array
fillSpace (temp); // Missing Line for function call
cout << "Expiration Date: " << temp << endl; // replace line with
Hope
this will Help you.
I have just recently started class inheritance in c++. While I was making a "Test" program, a error occurred with the cout statement. No clue how to fix it and would be appreciate your response.
#include <iostream>
using namespace std;
class Power{
public:
void isWeak(){
cout << " Weak"<< endl;
}
void isStrong(){
cout << " Strong" << endl;
}
};
class Person:public Power{};
class Person2:public Power{};
int main(){
Person human;
Person2 human2;
cout << "Human is " << human.isWeak() << endl; //error
cout << "Human 2 is " << human2.isStrong() << endl; //error
system("pause");
return 0;
}
the main()'s cout statement has that error between the output and human
Change the functions to
char const *isWeak(){
return " Weak";
}
char const *isStrong(){
return " Strong";
}
As currently defined, both functions have void return type, which means the cout statements within main are trying to print void, which doesn't make sense, and is the cause of the error.
You are attempting to print a void:
cout << "Human is " << human.isWeak() << endl;
is the same as typing
cout << "Human is " << void << endl;
Which will not compile. What you need to do is define your functions in either of the following ways:
class Power
{
public:
std::string isWeak()
{
return std::string(" is weak");
}
std::string isStrong()
{
return std::string(" is strong");
}
};
Or, change your code:
cout << "Human is ";
human.isWeak();
cout << endl;
cout << "Human 2 is ";
human2.isStrong();
cout << endl;
Problem is with isWeak() and isStrong() return type. these two functions return void and you are trying to print it. you can try this-
cout << "Human is " ;
human.isWeak();
cout << endl;
cout << "Human 2 is " ;
human2.isStrong();
cout << endl;
You're trying to 'print' a 'void' statement in cout << "Human is " << human.isWeak() << endl;
You'll need to change your isWeak and isStrong functions to return a std::string/const char* or change the way you call them:
to string:
const char* isWeak() {
return " Weak";
}
// then you can do
cout << "Human is " << human.isWeak() << endl;
Or change the way you call the function:
cout << "Human is ";
human.isWeak();
Your isWeak and isStrong functions are void they do not return anything; calling cout << human.isWeak() is expecting isWeak to return something (an int, string, double, etc.).