I have the declared the following struct
:
const struct DATABASE_ALREADY_EXISTS {
const int Code = 2001;
const char Str[] = "Database already exists.";
};
But then when I pass it to the function:
DATABASE_ALREADY_EXISTS DB_ERR;
error_output(DB_ERR.Str, DB_ERR.Code);
It throws the following error(Visual Studio 2013):
cannot specify explicit initializer for arrays
Here is the declaration error_output:
template<class D>
char* error_output(D err_str, int err_code = 0)
{
return "(" + err_code + ") " + err_str;
}
How should I change the definition Str
member of the struct
to eliminate such error?
答案 0 :(得分:1)
我认为你可以修改你的代码,如下所示;由于返回.video-container {
position: relative;
padding-bottom: 100%;
height:100%;
}
.video-container iframe, .video-container object, .video-container embed {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
margin:0px;
}
没有任何意义,因此您无法在"(" + err_code + ") " + err_str;
两个+
上应用char *
运算符。
#include <string>
#include <iostream>
#include <cstring>
#include <sstream>
struct DATABASE_ALREADY_EXISTS {
DATABASE_ALREADY_EXISTS(const char* s) : Str(s) {}
static const int Code = 2001;
const char *Str;
};
template<class D>
const char* error_output(D err_str, int err_code = 0)
{
std::istringstream is;
is>>err_code;
std::string str;
str += "(";
str += is.str();
str += ") ";
str += err_str;
return str.c_str();
}
int main(void)
{
DATABASE_ALREADY_EXISTS DB_ERR("Database already exists.");
const char *res = error_output(DB_ERR.Str, DB_ERR.Code);
std::cout<<res<<std::endl;
return 0;
}
答案 1 :(得分:1)
#include <iostream>
using namespace std;
struct Error {
private:
static char Buf[1024];
public:
int Code;
char* Str;
char* DisplayString() const {
sprintf_s(Buf, 1024, "(%d) %s", Code, Str);
return Buf;
}
friend ostream& operator<<(ostream& os, const Error& rhs) {
return os << rhs.DisplayString();
}
};
char Error::Buf[1024];
#define DEC_ERROR(name, code, str) Error name = { code, str }
DEC_ERROR(DATABASE_ALREADY_EXISTS, 2001, "Database already exists.");
DEC_ERROR(UNICORN_WITHOUT_HORN, 2002, "Unicorns should have a horn.");
int _tmain(int argc, _TCHAR* argv[]) {
cout << DATABASE_ALREADY_EXISTS << endl;
cout << UNICORN_WITHOUT_HORN << endl;
}
输出:
(2001)数据库已经存在
(2002)独角兽应该有一个角。