Thread: Stuck on 'undefined reference '

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    39

    Stuck on 'undefined reference '

    This should be easy to fix but im in the dark.

    LineCounter.h

    Code:
    #ifndef LINE_COUNTER_H
    #define LINE_COUNTER_H
    
    class LineCounter{
    	private:
    		char pC;
    		char pNC;
    		char C;
    		char NC;
    		static int CommentCount;
    		static int LineCount;
    		bool ReachedEndOFLine;
    	public:
    		void countLines();
    		int getCommentCount();
    		int getLineCount();
    };
    
    #endif
    LineCounter.cpp

    Code:
    #include "LineCounter.h"
    
    int CommentCount = 0;
    int LineCount = 0;
    
    void LineCounter::countLines(){
    	
    }
    
    int LineCounter::getCommentCount(){ return CommentCount;}
    int LineCounter::getLineCount(){ return LineCount;}
    Main.cpp

    Code:
    #include <iostream>
    #include <fstream>   // file I/O
    #include "LineCounter.h"
    
    using namespace std;
    
    int main(int argc, char *arg[]){
    	
    	if (argc < 1){
    		cout << "Usage is 'sloc files[]'";
    		return 0;
    	}else{
    		ifstream SourceFile(arg[1], ios::in);  // declare and open
    	}
    }
    Compile command
    Code:
     c++ LineCounter.cpp Main.cpp -o Program.exe
    Error:

    Code:
    /tmp/cc0kkubh.o: In function `LineCounter::getCommentCount()':
    LineCounter.cpp:(.text+0xa): undefined reference to `LineCounter::CommentCount'
    /tmp/cc0kkubh.o: In function `LineCounter::getLineCount()':
    LineCounter.cpp:(.text+0x14): undefined reference to `LineCounter::LineCount'

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > int CommentCount = 0;
    > int LineCount = 0;
    These need the class name in front of them as well.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    903
    Quote Originally Posted by Salem
    > int CommentCount = 0;
    > int LineCount = 0;
    These need the class name in front of them as well.
    Only for static variables. For non-static variables I would use the constructor to initialize them.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. error: undefined reference to pointer
    By blacknail in forum C Programming
    Replies: 7
    Last Post: 09-15-2008, 04:46 AM
  2. Problem with Makefile
    By pinkprincess in forum C Programming
    Replies: 3
    Last Post: 06-24-2007, 09:02 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. undefined reference to `_impure_ptr'
    By Sargnagel in forum C Programming
    Replies: 2
    Last Post: 07-13-2003, 08:31 AM
  5. compiler problem
    By sofarsoclose in forum C Programming
    Replies: 3
    Last Post: 07-10-2003, 11:39 AM