Two things. First I need to resolve the linker errors I'm getting and I also need to learn how to create a text file in a C++ project file.

I'm working on a stacks project with classes and I'm running into linker errors. Specifically it says: [Linker error] undefined reference to `Stack::Stack()'. I have a similar error for all the functions in my implementation file. I've tried several solutions and they've all failed. Please help. Thanks.

The other problem. Since I was having the above problem, I decided to create a C++ project file. Usually, when I start working on an assignment, I create one source file and then make sure all subsequent files go into the same folder on my hard drive so I can compile them all at the same place. My text file is also there.
But now that I have created a project file with all my files in there. But I can't seem to figure out how to include a text file in a project file. I created a text file using notepad and saved it in the folder containing the project file but I see no output file.

I have my input and output file as:
infile("mystack.txt");
outfile(mystackout.txt");

Please help. Thanks.

Recommended Answers

All 12 Replies

I suspect you're creating the stack using the wrong syntax. May we see the line of code in which you try to create the stack?

I suspect you're creating the stack using the wrong syntax. May we see the line of code in which you try to create the stack?

This is my main file:

#include <iostream>
#include <fstream>
#include "mystack.h"


using namespace std;


ifstream infile;
ofstream outfile;

int main ()
{
    Stack mystack, stack1, stack2;
    int elem;
    
    infile.open("mystack.txt");
    outfile.open("mystack out.txt");
    
    mystack.initializeStack();

    for (int i =0; i < maxSize; i++)
    {
           infile >> elem;
           mystack.pushStack(elem); //add elements to stack
           outfile << elem << " ";
    }
    
    //mystack.printStack(stack1); //I'm trying to pass the elements in mystack to the
                                //printStack function
    
   // mystack.exchangeTopAndBottom(stack1);
    
   // mystack.sumStack(stack1);
    
  //  mystack.OddElem(stack1);
    
  // mystack.fillStack(maxSize);
    
    //mystack.commonStack(stack1, stack2);
    
   // mystack.intersectStack(stack1, stack2);
    
    
    infile.close();
    outfile.close();
    system ("pause");
    
    return 0;
}

Header file

#ifndef _mystack_H
#define _mystack_H

#include <iostream>
#include <fstream>




const unsigned int maxSize = 10;

class Stack
{
      public:
                Stack(); //constructor
      
                ~Stack(); //Destructor

                bool isEmptyStack();
                
                bool isFullStack();
                
                void pushStack(int newItem);
                
                void popStack(int item);
                
                void initializeStack();
                
                void fillStack(int numSize);
                
                void exchangeTopAndBottom(Stack &stk);
                
                void printStack(Stack &stk);
                
                int sumStack(Stack &stk);
                
                void OddElem(Stack &stk);
                
                void commonStack(Stack &stk1, Stack &stk2);
                
                void intersectStack(Stack &stk1, Stack &stk2);

       private:
                int maxSize;  //variable to store the maximum stack size
                int stackTop; //variable to poit to the top of the stack
                int *arrList;//pointer to the array that holds the stack 
                              //elements
        
};      

#endif

Oh, I see. I thought you were using the stack from the std library. So, your header file gives the prototype for the stack default constructor ( Stack(); ). Where have you actually written the code for it? The linker can't find it.

Oh, I see. I thought you were using the stack from the std library. So, your header file gives the prototype for the stack default constructor ( Stack(); ). Where have you actually written the code for it?

Here is part of the implementation file:

#include <iostream>
#include <fstream>
#include "mystack.h"




using namespace std;

ifstream infile;
ofstream outfile;


//Constructor
Stack::Stack()
{
      stackTop = 0;
}


//Destructor
Stack::~Stack()
{
      stackTop = 0;
}


//Function to determine whether the stack is empty
bool Stack::isEmptyStack()
{
     return (stackTop == 0);
}


//Function to determine whether the stack is full
bool Stack::isFullStack()
{
     return (stackTop == maxSize);
}


//Initializes stack
void Stack::initializeStack()
{
     stackTop = 0;
}


//Fills a stack
void Stack::fillStack(int numSize)
{
     int elem;
     Stack stack;
     
     for (int i =0; i < numSize; i++)
     {
           infile >> elem;
           stack.pushStack(elem);
           outfile << elem << " ";
     }
     printStack(stack);
}



//Function to add a new item to the stack
void Stack::pushStack(int newItem)
{ 
     if (!isFullStack())
     {
        arrList[stackTop] = newItem;
        stackTop++;
     }
     else
         outfile <<"Cannot add to a full stack. " << endl;
}

Well that constructor implementation looks fine. The linker would not find it if the compiled object code was not handed to the linker, which means you're not compiling this code (I would guess it's named stack.cpp or the like), or you are compiling it and your linker is not being directed towards the compiled object code.

I presume you're using some kind of IDE. Is stack.cpp in the project or whatever your IDE calls the group of files? Is stack.cpp compiling properly?

Well that constructor implementation looks fine. The linker would not find it if the compiled object code was not handed to the linker, which means you're not compiling this code (I would guess it's named stack.cpp or the like), or you are compiling it and your linker is not being directed towards the compiled object code.

I presume you're using some kind of IDE. Is stack.cpp in the project or whatever your IDE calls the group of files? Is stack.cpp compiling properly?

Does it matter if my main.cpp, mystack.cpp and mystack.h are in a project file? I am asking because I created each of these files as a source file and saved it in one folder. Then I compiled main and mystack.cpp. main.cpp gives me all the linker errors and mystack.cpp also gives me this linker error:

[Linker error] undefined reference to `WinMain@16'

So I tried to create a project file so all my files can be in one place. I compiled it and I'm also getting other errors.
Could that be the problem? That I need to have all the files saved under one project instead of writing each as an individual source file and saving it in a folder?

[Linker error] undefined reference to `WinMain@16'

Looks like you are using an IDE to build a windows application. What IDE are you using? I am going to assume visual c++ of some sort, so, may I suggest creating a new console application rather than the default choice of Windows Application on the new project menu, then add the files.
I have seen some people need a different project for an implementation file, have to compile the implementation file into object code, then go into the project properties and add the appropriate object files to the linker options, etc. Quite a hassle. My suggestion, is to install a command line compile like MingW and just use something like this (depends on what your filenames are)

g++ -c mystack.cxx
g++ main -o myprogram.exe mystack.o

I hope this steers you in the right direction.

The linker needs to be fed all the compiled object files at once. If you're using some kind of IDE, this probably means they all have to be in the same project or whatever your IDE calls it. Putting them all in different directories makes no difference at all. It only matters which compiled object files you feed to your linker.

The linker needs to be fed all the compiled object files at once. If you're using some kind of IDE, this probably means they all have to be in the same project or whatever your IDE calls it. Putting them all in different directories makes no difference at all. It only matters which compiled object files you feed to your linker.

Makes sense. I am using Bloodshed Dev C++ to compile my project.
So do I go to Project->Project options->Parameters->Linker and add the object files?
That's what I've read from other posts. If I do that, then how do I include my text file so the compiler compiles it together with the object files?

I found out what was wrong. If you look at the header file I posted, I had all these extra function declarations inside there. I included these functions in the implementation file as well.

void fillStack(int numSize);

void exchangeTopAndBottom(Stack &stk);

void printStack(Stack &stk);

int sumStack(Stack &stk);

void OddElem(Stack &stk);

void commonStack(Stack &stk1, Stack &stk2);

void intersectStack(Stack &stk1, Stack &stk2);

Only initializeStack(), isEmptyStack(), isFullStack(), pushStack(elem) and popStack(elem)were supposed to be in the header file.
Also, only the function definitions of these functions were supposed to be in the implementation file. The rest of the functions goes into the main file. Once I did this rearrangement, the linker error left and my program is running. I hope this helps someone avoid this mistake.
Thanks Moschops and Stazloz for helping. I really appreciate it. Thanks.

In light of the mistake I made, what could have caused the linker error? Was it because of the conflicting information in the header file and implementation file? I would like to know and it might help someone too. Thanks

You dont need to include the text files with the project, just make sure they are in the same directory as the executable, it will not be used during compilation, only at run time.
Edit: Oh i didnt see your last post on a new page, I would assume so, multiple definitions/declarations can be bad, sometimes it may even give errors that would indicate the compiler thought you were overloading some functions. Also, look into creating your own namespaces, this could prevent multiple function definitions if you were to include someone else's code that coincidentally had functions/classes under the same names.

Ok. Thanks a lot. You are right about putting the text file in the same directory as the executables. Once I did that, it created the output file with some values I wanted to see. I'll look into namespaces too. Thanks again.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.