我的C ++代码存在一些问题。我现在试图拆分一个头文件,它有一些char指针的定义和声明(以前的字符串,但我一直有一些CRT问题,所以将它们改为char指针)。由于我已将它们更改为char指针,因此我发现了重定义错误。
Test.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();
};
}
Test.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错误C2086:'const char * DataVerifier :: Structure :: strHTMLTemplate':重新定义c:\ users \ suzan \ documents \ visual studio 2013 \ projects \ test1 \ test1 \ testconstantsheader.cpp 7 1 Test1 错误3错误C2086:'const char * DataVerifier :: Structure :: strHTMLTemplate':重新定义c:\ users \ suzan \ documents \ visual studio 2013 \ projects \ test1 \ test1 \ testconstantsheader.cpp 7 1 Test1
答案 0 :(得分:3)
您的标头文件包含具有外部链接的对象strHTMLTemplate
的定义。这就是导致多个定义错误的原因。
通过添加关键字strHTMLTemplate
extern
的定义更改为非定义声明
// TestConstantsHeader.h
namespace DataVerifier
{
namespace Structure
{
extern const char *strHTMLTemplate;
}
}
最重要的是,将指针声明为const char *const strHTMLTemplate
可能是有意义的。它看起来不应该是可修改的。