Unresolved Externals in a string

I started to work with SDL and decided to write my own class for working with it, for showing errors, loading images, etc.
I made a class called MySDL containing sub-classes like the class Errors that should write errors into a file that I can set. I want this class to be static, to use it without creating any object out of it so I declared every member of the class static and set the destructor to zero so that I can't make an object out of it. The problem is when I use the string that holds the name of the file (error_log.txt for example) in an ofstream object to create the file and write into it, it gives the error of "Unresolved Externals" and this warning

Warning 1 warning LNK4098: defaultlib 'msvcrt.lib' conflicts with use of other libs; use /NODEFAULTLIB:library C:\Users\Marek\Desktop\C++\MSVC\SDL_Project 0\SDL_Project 0\MSVCRTD.lib(cinitexe.obj)

Here is the *.h file of the class
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#ifndef _FUNC_H_
#define _FUNC_H_

class MySDL
{
public:
	class Errors
	{
	public:
		Errors() { ShowDate = true; FileName = "error_log.txt"; }
		virtual ~Errors() = 0;
		
		static void setWriteFile(std::string Filename) { FileName = Filename; }
		static std::string getWriteFile() { return FileName; }

		static void WriteToFile(std::string Message);

		static void isShowDate(bool val) { ShowDate = val; }

	private:
		static bool ShowDate;
		static std::string FileName;
	};



private:
};

#endif 


and here is the *.cpp file

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <SDL.h>
#include <SDL_image.h>
#include <SDL_ttf.h>
#include <SDL_mixer.h>

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <math.h>
#include <string>
#include <sstream>
#include <fstream>

#include "func.h"

//Errors Class
MySDL::Errors::~Errors() { }
void MySDL::Errors::WriteToFile(std::string Message)
{
	std::ofstream file;
	std::string filename_copy = Errors::FileName;
	file.open(filename_copy,std::ios::app);
	
	file << Message << " ";
	file << '\n';
	file.close();
}


I always try to figure a problem by myself before asking, but this took me a lot of time and I didn't find the error... I tried renaming the string FileName to different names... I gave to the .open() instead of the string FileName another string, worked fine... I even made a char for the string to copy into but as I used the string ... "Unresolved externals".
It's is truly an EPIC FAIL that my error showing class has errors in itself...
"Unresolved external" usually means a function has a prototype but the linker can't find a body for the function.

The linker will tell you exactly which function it's looking for and can't find. It's part of the error message (if you could post the entire error message, that would help).
Warning 1 warning LNK4098: defaultlib 'msvcrt.lib' conflicts with use of other libs; use /NODEFAULTLIB:library C:\Users\Marek\Desktop\C++\MSVC\SDL_Project 0\SDL_Project 0\MSVCRTD.lib(cinitexe.obj)

Error 2 error LNK2001: unresolved external symbol "private: static class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > MySDL::Errors::FileName" (?FileName@Errors@MySDL@@0V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@A) C:\Users\Marek\Desktop\C++\MSVC\SDL_Project 0\SDL_Project 0\func.obj

Error 3 error LNK1120: 1 unresolved externals C:\Users\Marek\Desktop\C++\MSVC\SDL_Project 0\Debug\SDL_Project 0.exe 1
Last edited on
You have to define static member variables in the source file.
1
2
bool MySDL::ShowDate;
std::string MySDL::FileName;
You can also initialize them here.


MySDL and Errors looks like they are being used more as a namespace than a class. When everything within a class is static it probably better to make it a namespace instead.
Thanks, it is working now, I got confused with the private so I thought I can't initialize them.
And I will use a namespace, this class within a class completely static was silly from the beggining I just didn't think about any other way...
Topic archived. No new replies allowed.