gpt4 book ai didi

c++ - 错误 C2086 : redefinition

转载 作者:行者123 更新时间:2023-11-28 00:13:31 31 4
gpt4 key购买 nike

我的 C++ 代码有一些问题。我现在正在尝试拆分一个头文件,该文件具有一些 char 指针的定义和声明(以前是字符串,但我遇到了一些 CRT 问题,因此将它们更改为 char 指针)。由于我已将它们更改为 char 指针,因此出现重定义错误。

测试.h

#pragma once
#define DllExport __declspec( dllexport )

#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <map>
#include <stdexcept>
#include "TestConstantsHeader.cpp"

namespace DataVerifier
{
class DllExport CDataVerifier
{
public:
CDataVerifier();
~CDataVerifier();

//! @brief A function that processes the messages
void Process();
};
}

测试.cpp

#include "Test.h"

using namespace DataVerifier;

CDataVerifier::CDataVerifier()
{
}

CDataVerifier::~CDataVerifier(){}

void CDataVerifier::Process()
{
const char* strTmp = Structure::strHTMLTemplate;
strTmp = "hello";
std::cout << strTmp;
}

TestConstantsHeader.h

#pragma once
#define DllExport __declspec( dllexport )

#include <iostream>

namespace DataVerifier
{
namespace Structure
{
const char *strHTMLTemplate;/* = "<doctype html>"
"<html lang=\"en\">"
"<head>"
"<meta charset=\"utf-8\"/>"
"<title>Data Verifier Test Results - {@testdesc}</title>"
"<style>"
"body {"
"\tbackground: none repeat scroll 0 0 #F3F3F4;"
"\tcolor: #1E1E1F;"
"\tfont-family: \"Segoe UI\",Tahoma,Geneva,Verdana,sans-serif;"
"\tmargin: 0;"
"\tpadding: 0;"
"}"
"h1 {"
"\tbackground-color: #E2E2E2;"
"\tborder-bottom: 1px solid #C1C1C2;"
"\tcolor: #201F20;"
"\tfont-size: 21pt;"
"\tfont-weight: normal;"
"\tmargin: 0;"
"\tpadding: 10px 0 10px 10px;"
"}"
"h2 {"
"\tfont-size: 18pt;"
"\tfont-weight: normal;"
"\tmargin: 0;"
"\tpadding: 15px 0 5px;"
"}"
"h3 {"
"\tbackground-color: rgba(0, 0, 0, 0);"
"\tfont-size: 15pt;"
"\tfont-weight: normal;"
"\tmargin: 0;"
"\tpadding: 15px 0 5px;"
"}"
"a {"
"\tcolor: #1382CE;"
"}"
"table {"
"\tborder-collapse: collapse;"
"\tborder-spacing: 0;"
"\tfont-size: 10pt;"
"}"
"table th {"
"\tbackground: none repeat scroll 0 0 #E7E7E8;"
"\tfont-weight: normal;"
"\tpadding: 3px 6px;"
"\ttext-align: left;"
"\ttext-decoration: none;"
"}"
"table td {"
"\tbackground: none repeat scroll 0 0 #F7F7F8;"
"\tborder: 1px solid #E7E7E8;"
"\tmargin: 0;"
"\tpadding: 3px 6px 5px 5px;"
"\tvertical-align: top;"
"}"
""
".textCentered {"
"\ttext-align: center;"
"}"
".messageCell {"
"\twidth: 100;"
"}"
"#content {"
"\tpadding: 0 12px 12px;"
"}"
"#overview table {"
"\tmax-width: 75;"
"\twidth: auto;"
"}"
"#messages table {"
"\twidth: 97;"
"}"
"</style>"
"</head>"
"<body>"
"<div id=\"big_wrapper\">"
"\t<h1>Test Results - {@testdesc}</h1>"
"\t<table>"
"{@eeddata}"
"\t</table>"
"</body>"
"</html>";*/
}
}

TestConstantsHeader.cpp

#include "TestConstantsHeader.h"

namespace DataVerifier
{
namespace Structure
{
const char *strHTMLTemplate = "<doctype html>"
"<html lang=\"en\">"
"<head>"
"<meta charset=\"utf-8\"/>"
"<title>Data Verifier Test Results - {@testdesc}</title>"
"<style>"
"body {"
"\tbackground: none repeat scroll 0 0 #F3F3F4;"
"\tcolor: #1E1E1F;"
"\tfont-family: \"Segoe UI\",Tahoma,Geneva,Verdana,sans-serif;"
"\tmargin: 0;"
"\tpadding: 0;"
"}"
"h1 {"
"\tbackground-color: #E2E2E2;"
"\tborder-bottom: 1px solid #C1C1C2;"
"\tcolor: #201F20;"
"\tfont-size: 21pt;"
"\tfont-weight: normal;"
"\tmargin: 0;"
"\tpadding: 10px 0 10px 10px;"
"}"
"h2 {"
"\tfont-size: 18pt;"
"\tfont-weight: normal;"
"\tmargin: 0;"
"\tpadding: 15px 0 5px;"
"}"
"h3 {"
"\tbackground-color: rgba(0, 0, 0, 0);"
"\tfont-size: 15pt;"
"\tfont-weight: normal;"
"\tmargin: 0;"
"\tpadding: 15px 0 5px;"
"}"
"a {"
"\tcolor: #1382CE;"
"}"
"table {"
"\tborder-collapse: collapse;"
"\tborder-spacing: 0;"
"\tfont-size: 10pt;"
"}"
"table th {"
"\tbackground: none repeat scroll 0 0 #E7E7E8;"
"\tfont-weight: normal;"
"\tpadding: 3px 6px;"
"\ttext-align: left;"
"\ttext-decoration: none;"
"}"
"table td {"
"\tbackground: none repeat scroll 0 0 #F7F7F8;"
"\tborder: 1px solid #E7E7E8;"
"\tmargin: 0;"
"\tpadding: 3px 6px 5px 5px;"
"\tvertical-align: top;"
"}"
""
".textCentered {"
"\ttext-align: center;"
"}"
".messageCell {"
"\twidth: 100;"
"}"
"#content {"
"\tpadding: 0 12px 12px;"
"}"
"#overview table {"
"\tmax-width: 75;"
"\twidth: auto;"
"}"
"#messages table {"
"\twidth: 97;"
"}"
"</style>"
"</head>"
"<body>"
"<div id=\"big_wrapper\">"
"\t<h1>Test Results - {@testdesc}</h1>"
"\t<table>"
"{@eeddata}"
"\t</table>"
"</body>"
"</html>";
};
};

真的不知道我在这里做错了什么......

这是我遇到的错误:

错误 2 error C2086: 'const char *DataVerifier::Structure::strHTMLTemplate' : redefinition c:\users\suzan\documents\visual studio 2013\projects\test1\test1\testconstantsheader.cpp 7 1 Test1错误 3 error C2086: 'const char *DataVerifier::Structure::strHTMLTemplate' : 重新定义 c:\users\suzan\documents\visual studio 2013\projects\test1\test1\testconstantsheader.cpp 7 1 Test1

最佳答案

您的头文件包含一个带有外部链接的对象 strHTMLTemplate定义。这就是导致多重定义错误的原因。

通过添加关键字extern,将头文件中strHTMLTemplate 的定义更改为非定义声明

// TestConstantsHeader.h

namespace DataVerifier
{
namespace Structure
{
extern const char *strHTMLTemplate;
}
}

最重要的是,将您的指针声明为 const char *const strHTMLTemplate 可能是有意义的。它看起来不应该是可修改的。

关于c++ - 错误 C2086 : redefinition,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31840201/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com