LNK2019&& LNK1120错误分割我的代码在多个文件 [英] LNK2019 && LNK1120 errors when splitting my code in multiple files

查看:156
本文介绍了LNK2019&& LNK1120错误分割我的代码在多个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码存储在包含 void main()函数的 main.cpp 文件中, class MyClass ,现在我想将其拆分为另一个文件。 IDE是Microsoft Visual Studio 2008 Professional。

My code is stored in a main.cpp file which contains the void main() function, and a class MyClass which I now want to split to another file. IDE is Microsoft Visual Studio 2008 Professional.

myclass.h

#include <tchar.h>
class MyClass {
public:
    static bool MyFunction (TCHAR* someStringArgument);
};

myclass.cpp b
$ b

myclass.cpp

#include <tchar.h>
class MyClass {
private:
    static bool someProperty;
    static void doSomeOneTimeCode () {
        if (!someProperty) {
            /* do something */
            someProperty = true;
        }
    }
public:
    static bool MyFunction (TCHAR* someStringArgument) {
        doSomeOneTimeCode();
        /* do something */
        return true;
    }
};
bool MyClass::someProperty = false;

main.cpp

#include <windows.h>
#include <stdio.h>
#include <tchar.h>
#include "myclass.h"
void main () {
    if (MyClass::MyFunction(TEXT("myString"))) {
        _tprintf(TEXT("Yay\n"));
    }
}

但是,当我尝试运行它时,两个链接器错误。

However, when I try to run it, I get two linker errors.


  • LNK2019:未解析的外部符号...(提及 MyClass :: MyFunction

  • LNK1120:1个未解决的外部

链接器错误?

推荐答案

你在这里声明了两个类。其中一个在 myclass.h ,另一个在 myclass.cpp 。尝试改用下面的代码:

You declared two classes here. One of them is in myclass.h and the other is in myclass.cpp. Try the following instead:

#ifndef myclass_h_included
#define myclass_h_included

#include <tchar.h>

class MyClass {
private:
    static bool someProperty;
    static void doSomeOneTimeCode ();
public:
    static bool MyFunction (TCHAR* someStringArgument);
};

#endif //!myclass_h_included



myclass.cpp



myclass.cpp

#include "myclass.h"

/*static*/ bool MyClass::someProperty = false;

void
MyClass::doSomeOneTimeCode() {
    //...
}
bool
MyClass::MyFunction(TCHAR* someStringArgument) {
    //...
}

main.cpp 可以保持不变。我会注意 UncleBens a>回复。一次性初始化代码应尽可能隐藏。

Your main.cpp can stay the same. I would pay attention to UncleBens reply as well. One time initialization code should be hidden if at all possible.

这篇关于LNK2019&amp;&amp; LNK1120错误分割我的代码在多个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆