collect2: ld returned 1 exit status

This is a discussion on collect2: ld returned 1 exit status within the C++ Programming forums, part of the General Programming Boards category; hey guys can anyone help me with this? I am getting this error: davidmiller@Bryce-Millers-MacBook-Pro.local $ make g++ -I ../include *.cpp ...

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    7

    collect2: ld returned 1 exit status

    hey guys can anyone help me with this?

    I am getting this error:

    davidmiller@Bryce-Millers-MacBook-Pro.local $ make
    g++ -I ../include *.cpp -o assignment3.exe
    Undefined symbols:
    "GenStack<char>::~GenStack()", referenced from:
    DelimChecker<char>::~DelimChecker()in cc3z2PH0.o
    DelimChecker<char>::~DelimChecker()in cc3z2PH0.o
    "GenStack<char>::GenStack()", referenced from:
    DelimChecker<char>:elimChecker()in cc3z2PH0.o
    "DelimChecker<char>::check(std::basic_string<c har, std::char_traits<char>, std::allocator<char> >)", referenced from:
    _main in cc3z2PH0.o
    ld: symbol(s) not found
    collect2: ld returned 1 exit status
    make: *** [all] Error 1

    main.cpp
    Code:
    #include "DelimChecker.h"
    using namespace std;
    
    
    
    
    int main(int argc, char** argv)
    {
        string sourceFile(argv[1]);
        DelimChecker<char>* test = new DelimChecker<char>();
        test->check(sourceFile); //i think error is happening here
        
        return 0;
    }

    GenStack.h
    Code:
    /**header file for GenStack class
     * @author Bryce Miller
     * @author mille245@mail.chapman.edu
     * @version 1.0 
     */
    
    
    #ifndef GEN_STACK_H
    #define GEN_STACK_H
    
    
     #include <iostream>
     #include <fstream>
     #include <string>
     #include <sstream>
     #include <exception>
     using namespace std;
     
     
    template <class T>
    class GenStack
    {
    private:
        unsigned int m_LIMIT;
        int m_size;
        T* m_theStack;
        int m_top;
        
        
    public:
        GenStack();
        GenStack(int theSize);
        virtual ~GenStack();
        
        void push(T data);
        T pop();
        T peek();
        bool isEmpty();
        bool isFull();
        int getSize();
    };
    
    
    #endif
    GenStack.cpp
    Code:
    /**stack class based on arrays
     * @author Bryce Miller
     * @author mille245@mail.chapman.edu
     * @version 1.0 
     */
    
    
     #include "GenStack.h"
     
    /** The default constructor
     * creates stack with generic size limit 10
     * @param none
     * @return no return value
     * @exception none
     */
    template <class T>
    GenStack<T>::GenStack()
    {
        m_LIMIT = 10;
        m_size = 0;
        m_theStack = new T[m_LIMIT];
        m_top = -1;
    }
    
    
    /** User-defined array size constructor -
     * Custom sized stack
     * @param allocate The amount of memory to allocate as an array
     * @return no return value
     * @exception none
     */
    template <class T>
    GenStack<T>::GenStack(int theSize)
    {
        
        m_LIMIT = theSize;
        m_theStack = new T[m_LIMIT];
        m_top = -1;
    }
    
    
    /** The destructor: deletes the stack
     * @param none
     * @return no return value
     * @exception none
     */
    template <class T>
    GenStack<T>::~GenStack()
    {
        delete[] m_theStack;
    }
    
    
    /** push method
     * @param data: a piece of data to add to the stack
     * @return no return value
     * @exception none
     * increments the size of stack
     */
    template <class T>
    void GenStack<T>::push(T data)
    {
        m_theStack[++m_top] = data;
        ++m_size;
    }
    
    
    /** pop method
     * @param none
     * @return the top piece of data on the stack
     * @exception none
     * decrements the size
     */
    template <class T>
    T GenStack<T>::pop()
    {
        --m_size;
        return m_theStack[m_top--];
    }
    
    
    /** peek method
     * @param none
     * @return shows the top piece of data of the stack
     * @exception none
     */
    template <class T>
    T GenStack<T>::peek()
    {
        return m_theStack[m_top];
    }
    
    
    /** isEmpty method
     * @param none
     * @return boolean, if the size is equal to 0
     * @exception none
     */
    template <class T>
    bool GenStack<T>::isEmpty()
    {
        return (m_size == 0);
    }
    
    
    /** isFull Method
     * @param none
     * @return boolean, if size equals the LIMIT
     * @exception none
     */
    template <class T>
    bool GenStack<T>::isFull()
    {
        return (m_size == m_LIMIT);
    }
    
    
    /** getSize Method
     * @param none
     * @return int, the size
     * @exception none
     */
    template <class T>
    int GenStack<T>::getSize()
    {
        return m_size;
    }
    DelimChecker.h
    Code:
    /**header file for DelimChecker
     * @author Bryce Miller, Stephen Ciauri
     * @author mille245@mail.chapman.edu
     * @version 1.0 
     */
    
    
    #ifndef DELIM_CHECKER_H
    #define DELIM_CHECKER_H
    
    
     #include <iostream>
     #include <fstream>
     #include <string>
     #include <sstream>
     #include <exception>
     #include "GenStack.h"
    
    
     using namespace std;
     
    
    
    template <class T>
    class DelimChecker: public GenStack<T>
    {
    private:
        
        
        
    public:
        void check(string file);
        bool isDelim(char c);            //done MAYBE
        bool isOpen(char c);            //done
        char getComplement(T theDelim);        //done
    };
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    #endif
    DelimChecker.cpp
    Code:
    /**checks the source code for open ended delims
     * @author Bryce Miller
     * @author mille245@mail.chapman.edu
     * @version 1.0 
     */
    
    
     #include "DelimChecker.h"
    
    
     
     
     template <class T>
     void DelimChecker<T>::check(string file)
    {
        char c;
        string temp;
        int line = 0;
        GenStack<T> stack = new GenStack<T>();
    
    
        try
        {    
            ifstream theFile (file.c_str());
            char tempChar[256];
    
    
            if (theFile.is_open())
            {
                while (theFile.good())
                {
                    theFile.getline(tempChar, 256);
                    ++line;
                    
                    //loops throught each char in the line
                    for (int i = 0; i < temp.length(); ++i)
                        if(isDelim(c=temp[i])) 
                        {
                            try {     // adds all open brackets to the stack
                                if (isOpen(c))
                                    stack.push(c);
                                //removes if brackets match up
                                else if(stack.peek() == ((T)getComplement(c)))
                                    stack.pop();
                                else {    //error if brackets dont match up
                                    cout<<"ERROR: line : "<<line<<" : type mismatch"<<endl;
                                    cout<<"expected "<<getComplement(stack.peek())<<", got "<<c<<endl;
                                    exit(0);
                                }
                            } catch (...) {    //error if extra closed bracket is missing an open bracket
                                    cout<<"ERROR: line : "<<line<<" : type mismatch"<<endl;
                                    cout<<c<<" is extra or missing "<<getComplement(c)<<endl;
                                    exit(0);
                                
                            }
                        }
                }
            }
            theFile.close();
            
        }
        catch (...)
        {
            cout<<"Error."<<endl;
        }
        
        if (stack.getSize() > 0) {
            cout<<"ERROR: line : "<<line<<" : unexpected end-of-file"<<endl;
            try {
                cout<<"expected "<<getComplement(stack.peek())<<endl;
            }catch (...) {}
            exit(0);
        }
        cout<<"no errors!"<<endl;
        
    }
     
     
    
    
     
    /** isDelim method, checks if the char is one of the delims
     * @param char, c to check if the char is a delim
     * @return bool, true if it is one of the delims
     * @exception none
     */
     template <class T>
     bool DelimChecker<T>::isDelim(char c) 
    {
        int length;
        char delims[] = {'(', ')', '{', '}', '[', ']'};
        length = (sizeof delims)/(sizeof(char));   //dertermines length of array
        for (int i = 0; i < length; ++i)
        {
            if (c == delims[i])
            {
                return true;
            }
        }
        return false;
    }
    
    
    /** isOpen method, checks if the delim was left open
     * @param char, the delim to check
     * @return bool, true if one of the delims is left open
     * @exception none
     */
    template <class T>
    bool DelimChecker<T>::isOpen(char c) 
    {
        int length;
        char delims[] = {'(', '{', '['};
        length = (sizeof delims)/(sizeof(char));   //dertermines length of array
        for (int i = 0; i < length; ++i)
        {
            if (c == delims[i])
            {
                return true;
            }
        }
        return false;
    }
    
    
    /** getComplement method, will get the brakets significant other
     * @param T, theDelim to find the complement of
     * @return char, the opporsite of the bracket in the param
     * @exception none
     */
    template <class T>
    char DelimChecker<T>::getComplement(T theDelim) 
    {
        char result = 0;
        int length;
        char delims[] = {'(', ')', '{', '}', '[', ']'};
        length = (sizeof delims)/(sizeof(char));   //dertermines length of array
        for (int i = 0; i < length; ++i)
        {
            if (theDelim == (delims[i]))
            {
                if (i%2 == 0)
                {
                    result = delims[i+1];
                }
                else
                {
                    result = delims[i-1];
                }
            }
        }
        return result;
    }


    thank you in advance!

  2. #2
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,263
    Code:
    Undefined symbols:
    "GenStack<char>::~GenStack()", referenced from:
    You declared a destructor for GenStack but you didn't put it in the header file. In fact, ALL of the contents of GenStack.cpp should be in GenStack.h.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #3
    Registered User
    Join Date
    Feb 2012
    Posts
    7
    I believe i did here in GenStack.cpp line 39-48

    Code:
    /** The destructor: deletes the stack * @param none
     * @return no return value
     * @exception none
     */
    template <class T>
    GenStack<T>::~GenStack()
    {
    	delete[] m_theStack;
    }

  4. #4
    Registered User
    Join Date
    Feb 2012
    Posts
    7
    and it is also declared in the header file as well

  5. #5
    Registered User
    Join Date
    Feb 2012
    Posts
    7
    Any other ideas?

  6. #6
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,263
    The problem is the code in the .cpp file is compiled, and because it is a template it's like it doesn't exist. This code must be visible in the places that USE the template, it is not like an ordinary function. Putting template code in a .cpp file does not work.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  7. #7
    Registered User
    Join Date
    Feb 2012
    Posts
    7
    so i just need to simply cup all the contents of the .cpp files into the header files? and delete the .cpp files?

  8. #8
    Registered User
    Join Date
    Feb 2012
    Posts
    7
    tried that and resolved that but now its:

    Code:
    davidmiller@Bryce-Millers-MacBook-Pro.local $ makeg++ -I ../include *.cpp -o assignment3.exe
    DelimChecker.h: In member function ‘void DelimChecker<T>::check(std::string) [with T = char]’:
    main.cpp:9:   instantiated from here
    DelimChecker.h:50: error: invalid conversion from ‘GenStack<char>*’ to ‘int’
    DelimChecker.h:50: error:   initializing argument 1 of ‘GenStack<T>::GenStack(int) [with T = char]’
    make: *** [all] Error 1

  9. #9
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,503
    Yes.
    Read this though, may explain in greater detail: [35] Templates ..Updated!.., C++ FAQ
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  10. #10
    Registered User
    Join Date
    Feb 2012
    Posts
    7
    thank you guys now just got other minor bugs

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. collect2: ld returned 1 exit status error??
    By blindchicken11 in forum C Programming
    Replies: 11
    Last Post: 11-07-2011, 08:38 PM
  2. error: collect2: ld returned 1 exit statu
    By tommantonela in forum C++ Programming
    Replies: 5
    Last Post: 12-13-2009, 12:40 AM
  3. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 09:18 PM
  4. cannot find -lpcap collect2: ld returned 1 exit
    By nasim751 in forum C Programming
    Replies: 0
    Last Post: 02-12-2008, 12:37 AM
  5. ERROR collect2: ld returned 1 exit status
    By meili100 in forum C++ Programming
    Replies: 13
    Last Post: 12-04-2007, 12:20 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21