0

I'm new to c++, this class will be used with a flex scanner. I'm keeping it simple here just to get it to compile but I'm getting the following message (none of other threads about this message seem to apply to my situation):

Undefined symbols for architecture x86_64: "Listing::lexicalError", referenced from: Listing::Listing() in listing.o Listing::displayErrorCount() in listing.o Listing::increaseLexicalError() in listing.o ld: symbol(s) not found for architecture x86_64

Listing.h

using namespace std;

class Listing
{

public:
  enum ErrorType {LEXICAL, SYNTAX, SEMANTIC};

Listing();

void appendError(ErrorType error, char yytext[]);

void displayErrorCount();

void increaseLexicalError();

private:

static int lexicalError;

};

Listing.cpp

#include <iostream>
#include <sstream>
using namespace std;

#include "Listing.h"


Listing::Listing()
{
  lexicalError = 0;
}

void Listing::appendError(ErrorType error, char yytext[])
{
    switch (error) {
        case LEXICAL:
            cout << "Lexical Error, Invalid Character " << yytext << endl;
            break;
        case SEMANTIC:
            cout << "Semantic Error, ";
        case SYNTAX:
            cout << "Syntax Error, ";

    default:
        break;
}
}

void Listing::displayErrorCount()
{
    cout << "Lexical Errors " << lexicalError << " ";

}

void Listing::increaseLexicalError()
{
  lexicalError++;
}

Thanks for any help compiling. I'm sure the c++ code isn't pretty but I'm learning ...

Here's the Makefile:

compile: scanner.o listing.o
    g++ -o compile scanner.o listing.o

scanner.o: scanner.c listing.h tokens.h
    g++ -c scanner.c

scanner.c: scanner.l
    flex scanner.l
    mv lex.yy.c scanner.c

listing.o: listing.cpp listing.h
    g++ -c listing.cpp
MayNotBe
  • 2,110
  • 3
  • 32
  • 47
  • Show us your make file – Jay Aug 30 '14 at 17:44
  • I don't think a static variable can be private.. but it should? anyway removing static seems to make the build process work. And makefile is not needed. the same error happens on VS. – Gizmo Aug 30 '14 at 17:55
  • possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – Captain Obvlious Aug 30 '14 at 17:58
  • So, defining it in the .cpp solved the problem ... for now! – MayNotBe Aug 30 '14 at 18:00
  • You should have a better `Makefile`. At the very least, compile with `g++ -Wall -g`. And take example e.g. from [this](http://stackoverflow.com/a/14180540/841108) – Basile Starynkevitch Aug 30 '14 at 18:21

1 Answers1

4

You have to define your static member variable lexicalError in your .cpp file:

#include <iostream>
#include <sstream>
using namespace std;

#include "Listing.h"

// here is the definition
int Listing::lexicalError = 0;

Listing::Listing()
{
  // not sure if you really want to do this, it sets lexicalError to zero
  // every time a object of class Listing is constructed
  lexicalError = 0;
}

[...]
Horstling
  • 2,131
  • 12
  • 14