简体   繁体   English

讨厌的链接错误

[英]Pesky Link Errors

I am using visual studio 2010 and believe I have a project settings issue. 我正在使用Visual Studio 2010,并且认为我遇到项目设置问题。 I have a header file that has some declarations in it: 我有一个头文件,其中包含一些声明:

definitions.h definitions.h

#include <string>

struct myStruct
{
    std::string x[4];
    std::string y[8];
};

void InitializeStructData();

extern myStruct data[12];

and the cpp file initializes my structure: 和cpp文件初始化我的结构:

definitions.cpp definitions.cpp

#include "definitions.h"
#include <string>

mySturct data[12];

void InitializeStructData()
{
    data[0].x[0] = "a";
    data[0].x[1] = "b";
    ....
    data[0].y[0] = "a";
    ....
    ....
    data[11].y[7] = "done initializing"';
}

and I have a form that has some buttons and things whose text I populate from the arrays depending on different circumstances: 并且我有一个包含一些按钮和东西的表单,我会根据不同的情况从数组中填充其文本:

myForm.cpp myForm.cpp

#include "definitions.h"

... 

//form initialization

As soon as I have two #include "definitions.h" statements I get link errors: 一旦有了两个#include "definitions.h"语句,我就会得到链接错误:

Error   1   error LNK2005: "class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > * Definitions" 
Error   2   error LNK1169: one or more multiply defined symbols found

Your question is missing the important part. 您的问题缺少重要的部分。

You have a std::string* Definitions in a header, that you forgot to use extern with. 您在标头中有一个std::string* Definitions ,但您忘记与extern一起使用。

Do you have your code (.h file) inside: 您是否在其中包含代码(.h文件):

#ifndef DEFINITIONS_H
#define DEFINITIONS_H

#endif

to help prevent you from defining it multiple times, if you have it included in multiple places? 如果您将它包含在多个地方,以防止您多次定义它?

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

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