简体   繁体   English

C ++,数组声明,模板,链接器错误

[英]C++, array declaration, templates, linker error

There is a linker error in my SW. 我的软件中存在链接器错误。 I am using the following structure based on h, hpp, cpp files. 我正在基于h,hpp,cpp文件使用以下结构。 Some classes are templatized, some not, some have function templates. 有些类是模板化的,有些则没有,有些具有函数模板。 The SW contains hundreds of included classes... 软件包含数百个包含的类...

Declaration: 宣言:

test.h
#ifndef TEST_H
#define TEST_H

class Test
{
   public: 
        template <typename T>
        void foo1();

        void foo2 ();
};

#include "test.hpp" 

#endif

Definition: 定义:

test.hpp
#ifndef TEST_HPP
#define TEST_HPP

template <typename T>
void Test::foo1() {}

inline void Test::foo2() {} //or in cpp file

#endif

CPP file: CPP文件:

test.cpp
#include "test.h"

void Test::foo2() {} //or in hpp file as inline

I have the following problem. 我有以下问题。 The variable vars[] is declared in my h file 变量vars []在我的h文件中声明

test.h
#ifndef TEST_H
#define TEST_H

char *vars[] = { "first", "second"...};

class Test
{
    public: void foo();
};

#include "test.hpp"

#endif

and used as a local variable inside foo() method defined in hpp file as inline. 并用作hpp文件中定义为内联的foo()方法内的局部变量。

test.hpp
#ifndef TEST_HPP
#define TEST_HPP


inline void Test::foo() {
    char *var = vars[0];   //A Linker Error
}

#endif

However, the following linker error occurs: 但是,发生以下链接器错误:

Error   745 error LNK2005: "char * * vars" (?vars@@3PAPADA) already defined in main.obj

How and where to declare vars[] to avoid linker errors? 如何以及在何处声明vars []以避免链接器错误? After including 包括后

#include "test.hpp"

it is late to declare it... 现在宣布已经晚了...

As I wrote, the software contains a lot of cpp, and hpp files included each other (all includes have been checked). 如我所写,该软件包含许多cpp,hpp文件彼此包含(所有包含文件都已被检查)。 It is not possible to send the whole example... 无法发送整个示例...

The main.obj represents a file which contains the main class. main.obj代表一个包含主类的文件。

Declare vars in the header with extern linkage 使用外部链接声明标头中的vars

extern const char* vars[];

and define it in exactly one source file 并在一个源文件中定义它

const char* vars[] = {"foo", "bar"};

Note the const , the conversion from string literals to char* is deprecated. 注意const ,不建议使用从字符串文字到char*的转换。 The way you have it now, you're violationg the One definition rule (You're redefining vars in every translation unit your header is included in). 您现在拥有的方式违反了“ 一个”定义规则 (您在包含标头的每个翻译单元中重新定义vars )。

I think you just need this in test.hpp 我认为您只需要在test.hpp

extern char *vars[];

...and this in test.cpp ...然后在test.cpp中

char *vars[] = { "first", "second"...};

I assume you are not declaring two classes both named Test . 我假设您没有声明两个都名为Test类。 If you are, that will cause you no end of problems. 如果是的话,那将不会给您带来麻烦。 You are not allowed to do this. 您不允许这样做。

So I assume the full declaration of class Test looks like this: 因此,我假设class Test的完整声明如下所示:

class Test
{
 public:
   void foo();

   template <typename T>
   void foo1();

   void foo2 ();
};

If this is the case, then your problem is clear. 如果是这种情况,那么您的问题就很清楚了。

You need to change the definition of vars . 您需要更改vars的定义。 You need to have this in test.h : 您需要在test.h拥有它:

extern char *vars[];

And this in test.cpp : 而这在test.cpp

char *vars[] = { "first", "second"...};

Otherwise the compiler will think you're trying to declare a new version of vars in every file. 否则,编译器会认为您正在尝试在每个文件中声明vars的新版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM