C++ cli C++/CLI错误:';值类型为;int";不能用于初始化类型为“的实体”;部门^“';
我的问题是,在CreateNewDepartment函数的“return temp;”上方的一行中,我遇到了一个奇怪的编译错误(VisualStudio中的红色下划线)。当我将鼠标悬停在“Department^temp(deptId,var[0],var[1]);”中的编译错误文本(“deptId,”)上时,弹出提示显示:C++ cli C++/CLI错误:';值类型为;int";不能用于初始化类型为“的实体”;部门^“';,c++-cli,C++ Cli,我的问题是,在CreateNewDepartment函数的“return temp;”上方的一行中,我遇到了一个奇怪的编译错误(VisualStudio中的红色下划线)。当我将鼠标悬停在“Department^temp(deptId,var[0],var[1]);”中的编译错误文本(“deptId,”)上时,弹出提示显示: a value type of "int" cannot be used to initialize an entity of type "Department ^" 以下
a value type of "int" cannot be used to initialize an entity of type "Department ^"
以下是我创建新对象的代码:
Department^ CreateNewDepartment(SQLRETURN retCode, SQLHANDLE hStmt)
{
int deptId;
String^ Name;
String^ Location;
System::String^ bufN;
char buf[256];
SQLINTEGER numBytes;
for (int i = 1; i <= 3; i++)
{
retCode = SQLGetData(
hStmt,
i, // COLUMN NUMBER of the data to get
SQL_C_CHAR, // DATA TYPE that you expect to receive
buf, // BUFFER to put the data that you expect to receive
255, // BUFFER size in bytes (-1 for null terminator)
&numBytes // SIZE in bytes of data returned
);
if (CHECK(retCode, "SqlGetData", false))
{
bufN = gcnew String((char *)buf);
if (i == 1)
{
std::string s = msclr::interop::marshal_as<std::string>(bufN);
deptId = std::stoi(s, nullptr, 0);
}
else if (i == 2)
{
Name = bufN;
}
else if (i == 3)
{
Location = bufN;
}
}
}
Department^ temp(deptId, Name, Location);
return temp;
}
很抱歉,如果这是一个简单的错误或愚蠢的问题,但我是CLI新手
Department^ temp(deptId, Name, Location);
显然应该是:
Department^ temp(gcnew Department(deptId, Name, Location));
虽然我不知道为什么。lol对于堆栈语义,第一个语法在没有
^
的情况下是正确的。第二种语法更常见于Department^temp=gcnew代码>。请记住,^
类似于*
,变量用(gc)new初始化。
Department^ temp(deptId, Name, Location);
Department^ temp(gcnew Department(deptId, Name, Location));