Unresolved external symbol C++

2019-01-28 09:40发布

I ahve a problem with a code below:

ProgrammSettings.h

#pragma once
static class ProgrammSettings
{
public:
    static int fd;
};

Settings.cpp

#include "ProgrammSettings.h"

static bool LoadSettings()
{
    ProgrammSettings::fd = 2; // here error Unresolved symbol!!
    return true;
}

What i'm doing wrong? Thanks!

标签: c++ class symbol
3条回答
叛逆
2楼-- · 2019-01-28 09:56

You need to add the following line to the start of your cpp file

 int ProgrammSettings::fd;
查看更多
Deceive 欺骗
3楼-- · 2019-01-28 10:03

Unlike instance variables that require only a declaration, static member variabs of the class must also be defined.

Currently, your code contains only a declaration. Add a definition of your static fd variable to a cpp file to fix the error:

int ProgrammSettings::fd;
查看更多
Viruses.
4楼-- · 2019-01-28 10:05

Static data members declarations in the class declaration are not definition of them You have forgot to add the definition to match your declaration of fd.
You must explicitly define your class's static data members.

查看更多
登录 后发表回答