• Create Account

### #ActualSpaceEmotion

Posted 25 July 2012 - 03:34 AM

I recently started learning C++ (I already know a lot about PHP and Java and have some knowledge in C) to create some games and discovered the SDL framework. After watching some tutorials in youtube I wanted to start myself making some basic stuff so I started setting up the environment using Netbeans.

First test were successfully (SDL_Init() and stuff) so I wanted to create some static classes (like i know them in PHP or Java) to create some Console output (using a prefix, date/time and so on) so I had three files in the end (console.h, console.cpp and main.cpp).

I got no compile errors but the linker said the following:
Undefined symbols for architecture x86_64:
"Console::prefix", referenced from:
Console::setPrefix(std::string) in console.o
Console::log(std::string) in console.o


Because I am a mac user I also tried using XCode (version 4), the same errors again. I don't know if its my code or just another problem, I really hope someone can help me in here.

For reference, this is the code I am using:

main.cpp ( the char** was added due to a tip in stackoverflow when I googled my problem, it is from my test with XCode)
#include <SDL/SDL.h>
#include <iostream>
#include "console.h"

int main(int argc, char** argv) {
Console::setPrefix("[Game]");

Console::log("Initializing SDL");
SDL_Init(SDL_INIT_EVERYTHING);

Console::log("Shutting down SDL");
SDL_Quit();

return 0;
}


console.h
#ifndef CONSOLE_H
#define CONSOLE_H
#include <string>
using namespace std;

class Console {
private:
/**
* The prefix used in the log output
*/
static string prefix;

public:
/**
* Sets the prefix for the output
* @param msg
*/
static void setPrefix(string msg);

/**
* Echoes the message with the prefix to the console
* @param msg
*/
static void log(string msg);
};
#endif


console.cpp
#include <iostream>
#include <string>
#include "console.h"
using namespace std;

void Console::setPrefix(string msg) {
prefix = msg;
}

void Console::log(string msg) {
std::cout << prefix << " " << msg;
}


(I don't know if its right to place this topic in here but since its a "beginners" problem I think this would be the best place in the forums.)

### #1SpaceEmotion

Posted 25 July 2012 - 03:26 AM

I recently started learning C++ (I already know a lot about PHP and Java and have some knowledge in C) to create some games and discovered the SDL framework. After watching some tutorials in youtube I wanted to start myself making some basic stuff so I started setting up the environment using Netbeans.

First test were successfully (SDL_Init() and stuff) so I wanted to create some static classes (like i know them in PHP or Java) to create some Console output (using a prefix, date/time and so on) so I had three files in the end (console.h, console.cpp and main.cpp).

I got no compile errors but the linker said the following:
Undefined symbols for architecture x86_64:
"Console::prefix", referenced from:
Console::setPrefix(std::string) in console.o
Console::log(std::string) in console.o


Because I am a mac user I also tried using XCode (version 4), the same errors again. I don't know if its my code or just another problem, I really hope someone can help me in here.

For reference, this is the code I am using:

main.cpp ( the char** was added due to a tip in stackoverflow when I googled my problem, it is from my test with XCode)
#include <SDL/SDL.h>
#include <iostream>
#include "console.h"

int main(int argc, char** argv) {
Console::setPrefix("[Game]");

Console::log("Initializing SDL");
SDL_Init(SDL_INIT_EVERYTHING);

Console::log("Shutting down SDL");
SDL_Quit();

return 0;
}


console.h
#ifndef CONSOLE_H
#define CONSOLE_H
#include <string>
using namespace std;

class Console {
private:
/**
* The prefix used in the log output
*/
static string prefix;

public:
/**
* Sets the prefix for the output
* @param msg
*/
static void setPrefix(string msg);

/**
* Echoes the message with the prefix to the console
* @param msg
*/
static void log(string msg);
};
#endif


console.cpp
#include <iostream>
#include <string>
#include "console.h"
using namespace std;

void Console::setPrefix(string msg) {
prefix = msg;
}

void Console::log(string msg) {
std::cout << prefix << " " << msg;
}


(I don't know if its right to place this topic in here but since its a "beginning" problem I think this would be the best place in the forums.)

PARTNERS