C++ Function call isn't recognized? (Array to Stack implementation)

Feb 4, 2016 at 1:24pm
Hey, so I seem to need a lot of help when it comes to C++. The purpose of this homework is to create a 1-dimensional array implementation for a Stack. First it reads a file and for every int it reads in the file it increments it increases count so that the array is allocated more space.

Then it gets read again to grab the integers to be put into the array with function push.

Anyways, in my main function - both push and pop are giving me a "Unable to resolve identifier" errors when I try to call them in main. Am I calling the correctly? Also, I'm not sure if my pop and push functions are correct? Thanks.

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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
  #include <iostream>;
#include <fstream>;
using namespace std;


  int *array;
    int top;
    int count;
    int data;


class Stack{
  
    
    Stack(int count){
        if (count <= 0)
        {
         throw string ("Stack must be 0 or greater.")   
        }
    
        array = new int[count];
        top = -1;
    
    }
    
    void push (int v);
    void pop();
   
    
    

};

//Function definitions

Stack::push(int v)
    {
     top++;
     array [top] = v;
   
    }
    
Stack::pop()
    {
     if (top == -1)
          throw string("Stack is empty");
       cout >> array[top];
     top--;   
    }


int main(int argc, char** argv, char* argv[])
{
    ifstream infile;
    infile.open ("StackData.txt");
    Stack MyStack;
    
    
    //Reading the file and then allocating memory for the size of the array.
    while (infile >> data)
    {
        
        getline(data);
        cout >> data;
        count ++;
               
    }
    
    
    
    //If the text file is empty, close it.
    if (infile.eof)
    {
     infile.close;   
        
    }
    
    
    //Open the file to read and populate the stack.
    infile.open ("StackData.txt");
    while (!infile.eof)
    {
        
        getline(data);
        MyStack.push(data);
        top++;
        
    }
    
    if (infile.eof)
    {
        pop(data);
        
        
    }
    
    
    if (infile.eof)
    {
     infile.close;   
        
    }
    
    delete[] array;
    
    
    
    return 0;
    
}
Last edited on Feb 4, 2016 at 1:25pm
Feb 4, 2016 at 1:30pm
Your functions are missing their types.

1
2
Stack::push(int v)
Stack::pop() 


should be

1
2
void Stack::push(int v)
void Stack::pop()
Feb 4, 2016 at 1:31pm
I changed that it, but is still giving me the same error :(
Last edited on Feb 4, 2016 at 1:33pm
Feb 4, 2016 at 1:33pm
Your function definitions are missing their return types. Line 18 is missing a semi-colon. The code above has many problems. Start at the first warning or error the compiler gives you, and fix them. The compiler is telling you already what is wrong with your code.
Feb 4, 2016 at 1:34pm
Still a few mistakes.

1
2
3
4
5
 Stack(int count){
        if (count <= 0)
        {
         throw string ("Stack must be 0 or greater.")   // missing a semicolon
        }


In classes, everything is by default private. You need to make your functions public by using the keyword public followed by colon.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Stack{
  
public: // make them public
    Stack(int count){
        if (count <= 0)
        {
         throw string ("Stack must be 0 or greater.")   
        }
    
        array = new int[count];
        top = -1;
    
    }
    
    void push (int v);
    void pop();

};
Last edited on Feb 4, 2016 at 1:34pm
Feb 4, 2016 at 1:59pm
Oh, thanks. Adding public fixed the error for push, but the pop function still seems to be unresolved?
Feb 4, 2016 at 2:01pm
Start at the first warning or error. Fix it. Attempt to recompile. Repeat until no warnings or errors.
Feb 4, 2016 at 2:02pm
You're messing up a very basic thing.


cout >> array[top]; // You want << for std::cout, not >>

You do this mistake at other places too, be careful.
Last edited on Feb 4, 2016 at 2:03pm
Feb 4, 2016 at 2:06pm
Seriously, this code has so many things for the compiler to complain about. When the compiler starts throwing errors at you, everything after that first error is untrustworthy. Start at the first error instead of picking one from the middle.
Feb 4, 2016 at 2:36pm
Yeah, sorry about that guys. I didn't try to Run/Build it and couldn't see the errors. I originally thought the only errors were the ones before I tried to compile it, so I was being extremely naive.

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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include <iostream>;
#include <fstream>;
using namespace std;

 
    
    int *array;
    int top;
    int count;

class Stack{
  

public:
    Stack(int count){
        if (count <= 0)
        {
         throw string ("Stack must be 0 or greater.");   
        }
    
        array = new int[count];
        top = -1;
    
    }
    
    void push (int v);
    void pop();
    bool isEmpty();
   
    
    

};

//Function definitions

    
void Stack::push(int v)
    {
     top++;
     array [top] = v;
   
    }
    
void Stack::pop()
    {
     if (isEmpty())
     {
          throw string("Stack is empty");
     }
       cout << array[top];
     top--;   
     
}
    

     
    bool Stack::isEmpty()
     {
         return (top == -1);
         
         
     }


int main(int argc, char *argv[])
{
    ifstream infile;
    infile.open ("StackData.txt");
    Stack MyStack;
    int data;
    
    
    //Reading the file and then allocating memory for the size of the array.
    while (infile >> data)
    {
        
        infile >> data;
        cout << data;
        count ++;
               
    }
    
    
    
    //If the text file is empty, close it.
    while (infile.eof())
    {
     infile.close();   
        
    }
    
    
    //Open the file to read and populate the stack.
    infile.open ("StackData.txt");
    while (!infile.eof())
    {
        
        infile >> data;
        MyStack.push(data);
       
        
    }
    
    if (infile.eof())
    {
        pop(data);
        
        
    }
    
    
    if (infile.eof())
    {
     infile.close();   
        
    }
    
    delete[] array;
    
    
    
    return 0;
    
}







I've fixed most of the errors I could find I guess. But lines 70 and 107 are still giving me problems. Trying to initiate MyStack is giving me an error. If I tried to change it to Stack::Stack MyStack ; it still gives me a weird error.
Last edited on Feb 4, 2016 at 2:37pm
Feb 4, 2016 at 3:32pm
You've defined a constructor for stack that takes an argument:

Stack(int count){

This means that the compiler won't automatically generate a default constructor. However, at line 70, you're trying to invoke a default constructor.

You've defined pop() to take no arguments, but at line 107, you're trying to pass an argument in.

giving me an error [...] it still gives me a weird error


I'm curious. When faced with a choice between telling us what those errors are, and not telling us - what made you decide that not telling us would be the better choice?
Last edited on Feb 4, 2016 at 3:34pm
Feb 4, 2016 at 3:56pm
Oh, I guess not telling was just me glossing over the fact that it kept saying "Unable to resolve identifier" errors I kept on receiving. I apologize.


So in line 70 should I change the call to invoke the constructor I defined Stack MyStack(); seems to remove the error but it seems wrong. Does it pass the value 0 and makes that the default for MyStack? Which gives me an error in line 100 that says "StackProject1.cpp:100:17: error: request for member 'push' in 'MyStack', which is of non-class type 'Stack()'
MyStack.push(data);"

And removing the argument in 107 to just be
1
2
3
4
5
6
 if (infile.eof())
    {
        pop();
        
        
    }
still gives me "Unable to resolve identifier pop".




Feb 4, 2016 at 4:00pm
You really need to take your time and concentrate. pop is part of the class. You have to use the object to call it. MyStack.pop();
Feb 4, 2016 at 4:23pm
So in line 70 should I change the call to invoke the constructor I defined


Yes. You've only defined one constructor, so that's the only one you can invoke.

Stack MyStack(); seems to remove the error but it seems wrong.


It's a weird quirk of C++ that that statement isn't interpreted as an object instantiation. It's actually interpreted as a function prototype, for a function called MyStack() that takes no arguments, and returns a Stack object. This is known as the "Most Vexing Parse".

However, even if it were interpreted as instantiating the object, it would be illegal - because, again, it's invoking a non-existant default constructor.

As I've already said:

You've defined a constructor for stack that takes an argument.
Feb 4, 2016 at 4:33pm
This is now more of a troubleshooting question on NetBeans I guess. Okay, I've gotten it to compile successfully. But on trying to run/test it, NetBeans pops up a Select Executable window. At first I assumed it was because it couldn't find the path of the file it was trying to open (which is in the same folder as the .cpp file).

Or maybe it has to do with an error in compiler installation? I followed everything about installing cygwin to the dot. Tested their Welcome sample project, and that ran.
Last edited on Feb 4, 2016 at 4:34pm
Feb 4, 2016 at 5:49pm
>> Stack MyStack(); seems to remove the error but it seems wrong.
> It's a weird quirk of C++ that that statement isn't interpreted as an object instantiation.
Suppose that that declaration is outside main, suppose that instead of `Stack' it said `int' or `double', ¿would there be any doubt about its meaning?


> At first I assumed it was because it couldn't find the path of the file it was trying to open
No, it would be your responsibility (your code) to handle that case.


> Okay, I've gotten it to compile successfully.
show your code.


> This is now more of a troubleshooting question on NetBeans I guess.
I don't know about netbeans.
Try to open a console, go to your project directory and execute your program.
Last edited on Feb 4, 2016 at 5:57pm
Feb 4, 2016 at 6:02pm
Suppose that that declaration is outside main, suppose that instead of `Stack' it said `int' or `double', ¿would there be any doubt about its meaning?

No, there wouldn't.

I'm not saying it's not logical. I'm just pointing it out as something that can confuse people who haven't come across it before.

Feb 4, 2016 at 10:01pm
Here's my code. Unfortunately I'm in my school library so I don't have access to a compiler besides using the site (http://www.tutorialspoint.com/compile_cpp_online.php) is proving difficult since I don't know how to get my test text file to test to see. It seems to compile there? But execution is proving difficult. I tried to create a new file, changing it's name to StackData.txt and dumping in the example integers in the file, but on execution it just says "main".


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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include <iostream>
#include <fstream>
using namespace std;

 
    
    int *array;
    int top;
    int count;

class Stack{
  

public:
    Stack(int count){
        if (count <= 0)
        {
         throw string ("Stack must be 0 or greater.");   
        }
    
        array = new int[count];
        top = -1;
    
    }
    
    void push (int v);
    void pop();
    bool isEmpty();
   
    
    

};

//Function definitions

    
void Stack::push(int v)
    {
     top++;
     array [top] = v;
   
    }
    
void Stack::pop()
    {
     if (isEmpty())
     {
          throw string("Stack is empty");
     }
       cout << array[top];
     top--;   
     
}
    

     
    bool Stack::isEmpty()
     {
         return (top == -1);
         
         
     }


int main(int argc, char *argv[])
{
    ifstream infile;
    infile.open ("StackData.txt");
   
    int data;
    
    
    //Reading the file and then allocating memory for the size of the array.
    while (infile >> data)
    {
        
        infile >> data;
        cout << data;
        count ++;
               
    }
    
     Stack MyStack(count);
    
    
    //If the text file is empty, close it.
    while (infile.eof())
    {
     infile.close();   
        
    }
    
    
    //Open the file to read and populate the stack.
    infile.open ("StackData.txt");
    while (!infile.eof())
    {
        
        infile >> data;
        MyStack.push(data);
       
        
    }
    
    if (infile.eof())
    {
        MyStack.pop();
        
        
    }
    
    
    if (infile.eof())
    {
     infile.close();   
        
    }
    
    delete[] array;
    
    
    
    return 0;
    
}







Last edited on Feb 4, 2016 at 10:04pm
Feb 4, 2016 at 10:33pm
¿why do you have so much whitespace?

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
  int *array; //these should clearly be members of the class, ¿why are they global?
    int top;
    int count;
    int data;
//...
    while (infile >> data)
    {
        infile >> data; //¿why are you reading twice?
        cout << data;
        count ++;          
    }
//...
    //If the text file is empty, close it.
    while (infile.eof()) //¿why did you put while?
    {
     infile.close();
    }
//...
    while (!infile.eof()) //loop on the reading instead
    {
        infile >> data;
        MyStack.push(data);   
    }
    if (infile.eof()) //so this wouldn't be necessary
    {
     infile.close();   
    }
Topic archived. No new replies allowed.