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.
#include <iostream>;
#include <fstream>;
usingnamespace 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 = newint[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;
}
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.
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 = newint[count];
top = -1;
}
void push (int v);
void pop();
};
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.
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.
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.
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?
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".
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.
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.
>> 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.
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".
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();
}