Calling method header file

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
Would you be so kind to perhaps include the code to your other files?

Edit: The error is obviously complaining about your function, which you dont even show us.
Last edited on
my bad, here are the files

stack.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#ifndef __STACK_H
#define __STACK_H

typedef float StackInfoType;

#define Testing_Stack
#ifdef Testing_Stack
#include <iostream>

using namespace std;

#endif

class Stack
{
public:
	Stack() 
	{
		top = 0;
	}
	
	void Clear() 
	{
		top = 0;
	}
	
	void Push(StackInfoType x) 
	{
		values[top++] = x;
	}
	
	StackInfoType pop() 
	{
		return values[--top];
	}
	
	bool IsEmpty() const 
	{
		return top == 0;
	}

	bool IsFull() const
	{
		return top == MAXSTACK;
	}

	enum {MAXSTACK = 80};

private:

	StackInfoType values[MAXSTACK];
	int top;

};

#endif 


stack.cpp
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;
}
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?
You dont need multiple mains. You can have 1 main. Then you can have 1 h file and 1 cpp file for each thing.

You usually want the class in the h file, and the function definitions in the cpp file.

Example:

.h file -

1
2
3
4
5
6
7
8
class Name
{
    //stuff
    void setName(std::string name);

private:
    std::string name;
};


.cpp file

1
2
3
4
5
6
#include "file.h" // fixed, thanks IDM

Name::setName(std::string nameArg)
{
    name = nameArg;
}


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.

Watch these videos to shed more light on the subject - https://www.youtube.com/watch?v=A4qXfQMZLwc&index=34&list=PL2DD6A625AD033D36

https://www.youtube.com/watch?v=NTip15BHVZc&index=15&list=PLAE85DE8440AA6B83
Last edited on

Just going to fix up a couple of things:

Name.hpp
1
2
3
4
5
6
7
8
9
10
#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;
};


https://en.wikipedia.org/wiki/Pragma_once


1
2
3
4
5
6
7
// Name.cpp
#include "Name.hpp" // quotes for your own header files

Name::setName(const std::string &nameArg)    
{
    name = nameArg;
}



@football52

typedef float StackInfoType;

I guess this is a precursor to learning templates:

1
2
3
4
5
6
7
8
9
template <typename T>
class Stack {
private:

	T values[MAXSTACK];
	int top = 0; // init here is same as member init list
public:
//.....
};



1
2
3
Stack<float>      MyFloatStack;
Stack<double>  MyDoubleStack;
Stack<int>        MyIntStack;


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 using namespace std; Google it to see why. Put std:: instead.

Good Luck !!
Last edited on
Topic archived. No new replies allowed.