GCC Undefined symbols for architecture x86_64 in C++ Constructor

I just started up a new project, and my class skeleton does not compile. The compiler error I am receiving is:

Undefined symbols for architecture x86_64:
  "SQLComm::ip", referenced from:
      SQLComm::SQLComm(int, std::__1::basic_string<char, std::__1::char_traits<char>,     std::__1::allocator<char> >) in SQLComm.o
  "SQLComm::port", referenced from:
  SQLComm::SQLComm(int, std::__1::basic_string<char, std::__1::char_traits<char>,     std::__1::allocator<char> >) in SQLComm.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

I have no idea why my code does not compile... Here's the class which errors:

SQLComm.h:

#ifndef __WhisperServer__SQLComm__
#define __WhisperServer__SQLComm__

#include <iostream>
#include <string>

class SQLComm {
public:
//Local vars
static int port;
static std::string ip;

//Public functions
void connect();
SQLComm(int sqlport, std::string sqlip);
~SQLComm();
private:

};



#endif /* defined(__WhisperServer__SQLComm__) */

And here's the SQLComm.cpp:

#include "SQLComm.h"


SQLComm::SQLComm(int sqlport, std::string sqlip){
ip = sqlip;
port = sqlport;
}

SQLComm::~SQLComm(){

}

void SQLComm::connect(){

}

The system is OSX10.9, and the compiler is GCC (in xCode).

If anyone could tell me why I am getting this error, I'd be very happy. Thanks in advance! :)

Upvotes: 0

Views: 963

Answers (2)

swegi
swegi

Reputation: 4102

You need to define your static class variables. Try

int SQLComm::port;
std::string SQLComm::ip;

in SQLComm.cpp.

Note: Most probably, you do not want to declare both variable as static class variables but as normal instance variables.

Upvotes: 2

Daniel Frey
Daniel Frey

Reputation: 56863

You have declared static variables but you haven't defined them. You need to add this

int SQLComm::port;
std::string SQLComm::ip;

to your SQLComm.cpp file.

Although... thinking about it this is probably not what you intended. You intended to declare non-static member variables, e.g., each instance of SQLComm should contain those variables, right? In that case, simply drop the static (and don't add the above to your .cpp file.

Upvotes: 2

Related Questions