簡體   English   中英

VS2010中的鏈接器錯誤(解決方案和靜態方法中的2個項目)

[英]Linker error in VS2010 (2 project in solution and static method)

我有錯誤LNK2019和LNK1120的問題。

我有兩個項目的解決方案。 在第一個程序中,我有我的程序,在第二個項目中,我有測試文件。

在第二個項目中,我可以包含頭文件(帶有類)。 而且我可以使用內聯方法。 但是...我已經在頭文件中定義了靜態私有方法,並在.cpp文件中實現了該方法。 如果我想測試使用此靜態私有方法的公共內聯方法,則會遇到LNK2019錯誤和致命錯誤LNK1120。

我不知道該怎么辦...對我來說看起來不錯,因為我可以在第一個項目中使用它而不會出現任何錯誤...

看一個例子...解決方案:

項目1:

// example.h
class Example {
public:
    void method() { static_method(); };
private:
    static void static_method();
};

// example.cpp
void Example::static_method() { /* implementation */ }

// main.cpp
void main()
{
    Example e;
    e.method();    // it works!
}

但...

項目2(在解決方案中):

// test.cpp
void main()
{
    Example e;
    e.method(); // it doesn't work (but if I add implementation of
                // Example::static_method() to header file example.h - it's OK)
}

你可以幫幫我嗎? [VS2010]

謝謝。

希望proj1是靜態庫,而proj2是exe或dll。 在這種情況下,您必須將proj1的引用添加到proj2,它將正常工作。 (最頂部的通用屬性/框架和參考;添加新參考)。

如果兩個項目均是exe,則必須拆分出帶有示例實現的庫,其余部分保留下來,然后在兩個項目中引用lib。

您也可以將其設置為DLL而不是靜態lib,但是必須使用適當的導出文件,並確保在單個目錄中生成文件以供執行。

嘗試在.h文件中實現靜態方法,例如:

// example.h
class Example {
public:
    void method1() { static_method(); };
private:
    static void static_method() { // implementation }
};

// example.cpp
/* nothing here yet... */

// main.cpp
void main()
{
    Example e;
    e.method1();    // it works!
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM