简体   繁体   中英

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. 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:

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

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.

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? 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). It is not possible to send the whole example...

The main.obj represents a file which contains the main class.

Declare vars in the header with extern linkage

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. 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).

I think you just need this in test.hpp

extern char *vars[];

...and this in test.cpp

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

I assume you are not declaring two classes both named 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
{
 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 . You need to have this in test.h :

extern char *vars[];

And this in test.cpp :

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

Otherwise the compiler will think you're trying to declare a new version of vars in every file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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