In my class we are converting a program from java we did last semester to c++ this semester. The way he is having us do it is to have all methods in the header files and testbed mains in the cpp files. Essentially we have one container with all the methods in the header and to test all the methods we are using a cpp file.
So my problem is I have all the methods done in the header file, now I'm working on the testbed main for the container. So far I have
1 2 3 4 5 6 7 8 9
#include "stack.h"
int main()
{
Stack s;
s.Push(10);
cout << "The number is " << s.pop << endl;
}
I have a push and pop method, and as far as I know the push works but I cant debug because the project doesnt even build. I get the error
f:\school\platteville\cs2640\prog1\stack.cpp(8): error C3867: 'Stack::pop': function call missing argument list; use '&Stack::pop' to create a pointer to member
The problem was indeed in main, but I glansed over it. I ran the program and found the problem, Always good to have everything relevant, because the problem could have very well not been in main.
cout << "The number is " << s.pop() << endl; // When you want to call a function, you need to use parenthesis. s.pop() and not s.pop
ahhh alright i passed right over that too many times.
Another question while your here too if you dont mind.
I have to make a stack.h stack.cpp which i already have, but I also have to make a queue.h and queue.cpp files and do the same thing the cpp file being the test bed main. Once I have them, how do I just run 1 file? Like I need to test queue.cpp is their a way I can just run that main? Since I will be having multiple mains, how do I just run 1?
Then in main. You just have to include the .h file, and then you can do exactly what you did in the main you have right now. If you create a Queue class teh same way, you just have to include that too, and you can test it in the same main.
#pragma once // not standard, but widely supported. Could use headers guards instead.
class Name
{
public:
//stuff
void setName(const std::string &name);
private:
std::string name;
};
I think you need some error checking code in your pop() function. If the stack is empty, it will try to return values[-1] which will likely to be garbage.
Try to avoid usingnamespace std; Google it to see why. Put std:: instead.