createstack identifier not found error c++

//impl.h
#include <string>
#include <stack>
#include <iostream>


using namespace std;


class VideoGame
{
public:
string title, genre, publisher;
int year;
VideoGame(string t, string p, string g, int y)
{
set_title(t);
set_genre(g);
set_publisher(p);
set_year(y);
}

void set_title(string t)
{
title = t;
}
void set_genre(string g)
{
genre = g;
}
void set_publisher(string p)
{
publisher = p;
}
void set_year(int y)
{
year = y;
}
void createstack(stack <VideoGame> s);


string get_publisher()
{
return publisher;
}
string get_title()
{
return title;
}
string get_genre()
{
return genre;
}
int get_year()
{
return year;
}


};

//impl.cpp

#include "impl.h"
void createstack(stack <VideoGame> s)
{

while (!s.empty())
{
VideoGame v = s.top();
cout << "TITLE : " << v.get_title() << endl;
cout << "YEAR : " << v.get_year() << endl;
cout << "GENRE : " << v.get_genre() << endl;
cout << "PUBLISHER : " << v.get_publisher() << endl;
cout << '\n';
cout << "------------------------" << endl;
s.pop();

}
}

//main.cpp

#include "impl.h"
int main() {


stack<VideoGame> s;
s.push(VideoGame("Tetris", "Nintendo", "Tile matching", 1984));
s.push(VideoGame("Super Mario", "Nintendo", "Platform", 1985));
s.push(VideoGame("Diablo", "Blizzard", "Hack and Slash", 1996));
s.push(VideoGame("Sonic", "Sega", "Platform", 1991));
s.push(VideoGame("The Sims", "Electronic Arts", "Life Simulation", 2000));
createstack(s);
s.pop();
return 0;

}


in c++ under the main function createstack(s); gives createstack identifier not found error .When i combine main.cpp and impl.cpp there is no error.But i have to seperate them. What is the solution to this error can anyone help? I try everything but i didnt solve this problem.
Last edited on
You are LINKING these files, aren't you? You don't just compile and link main.cpp alone.

Something like
g++ main.cpp impl.cpp
if you are using the command line.
@bartuasian
Yes, try linking them with the compiler, clang++ or gnu++, whichever one you have (I have both). You may also want to set various flags:

You don't compile header files separately; you #include them in the driver and implementation, and your compiler should automatically compile them into the executable with the others.

Are you using c++ or g++ for compiling, or are you using an IDE? I would try using g++ or c++ in the command line, and use this format:

c++ -std=c++2a -g -o executable program implementation

or
g++ -std=c++2a -g -o executable program implementation


The -g flag is for debugging, you can leave that out if you don't need to debug. The -o flag sets the name of the executable; if you leave it out, it defaults to a.out.

executable is whatever you want to call your exec,
program is the name of your driver program, like "rectangleProgram.cpp", and
implementation is the name of your class implementation file, like "rectangle.cpp.

Or:
g++ -std=c++2a -Wall -Wextra -pedantic program.cpp implementation.cpp


-Wall This enables all warnings; I always use it because that way I don't get unexpected results when I run my programs.
-Wextra Enables extra warnings.
-pedantic Honestly, I am not entirely sure what that does...I will look into it and when I find something, I'll post back.

The driver comes first, then the executable. At least that's how my g++ manual says to do it. If it works the way you do it, then you can ignore what I said.
Last edited on
On most Unix systems c++ is the default compiler, typically GCC.
Clang's executable is typically called clang++

P.S.:
-pedantic means something like "Disable (or warn me about) language extensions which might change the behavior of an otherwise acceptable program."

Google excludes results that contain search terms that are prefixed with a hyphen.
For example, the query GCC -pedantic returns results that contain the word "GCC" but do not contain the word "pedantic".

To fix it, put -pedantic in double quotes. So the right way to find the docs is to search for
GCC "-pedantic"
You could also just read the man page, but YMMV.
Last edited on


agent_max wrote:
-Wall This enables all warnings;


No it doesn't - enables most of the commonly used ones.

It is worth reading the manual despite there being zillions of options. There are other options that are not enabled with Wall and Wextra that are useful.

There is also -Werror which turns warnings into errors - this forces one to fix all warnings in order to get a successful compilation.

When compiling one can use *.cpp to compile all cpp files in that directory. In my mind this better than typing out all the cpp file names. Note to the OP: one needs to compile all the cpp files in your project.

Once one gets more advanced (more files to compile), then some sort of make system is desirable.
@mbozzi,
Thanks! I forgot about using the man page, probably because I don't use them often.

@TheIdeasMan,
Ah. Thanks for the correction! In retrospect, I should probably make an attempt to be more informed before suggesting stuff like that...or just stick to what I know ;)

I do use Makefiles occasionally when I have multiple header files and implementations. The resource I used was: https://www.cs.colby.edu/maxwell/courses/tutorials/maketutor/

But that *.cpp thing looks a heck of a lot easier than creating a Makefile, I will try that out!
@agent max

Often some of the IDE's will use a make file implicitly. For example with QtCreator there is a project file which it uses to create a shellscript (Linux) which does the actual make. I haven't used VS much, but it may have the same sort of thing in there somewhere.

The *.cpp is just an artifact of the shell (an expansion) - one could use other aspects of it, like: [a-c]*.cpp which would only do cpp files that start with a,b, or c. One can have multiple file expansion specs to handle different things like c and cpp files.

As for some of the other warnings, there is one that warns when not all of the enum values are used in a switch statement. You should have a read to discover some other worthwhile warnings to have on.
Topic archived. No new replies allowed.