Stuck on 'undefined reference '

This is a discussion on Stuck on 'undefined reference ' within the C++ Programming forums, part of the General Programming Boards category; 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: ...

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,750
    > 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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, 05:46 AM
  2. Problem with Makefile
    By pinkprincess in forum C Programming
    Replies: 3
    Last Post: 06-24-2007, 10:02 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  4. undefined reference to `_impure_ptr'
    By Sargnagel in forum C Programming
    Replies: 2
    Last Post: 07-13-2003, 09:31 AM
  5. compiler problem
    By sofarsoclose in forum C Programming
    Replies: 3
    Last Post: 07-10-2003, 12:39 PM

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