我的问题是在上面的线上"返回temp;"在我的CreateNewDepartment函数中,我得到一个奇怪的编译错误(Visual Studio中的红色下划线)。当我在" Department ^ temp(deptId,var [0],var [1]);"中鼠标悬停在编译错误文本(" deptId,")上时弹出提示说:
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;
}
部门的定义如下:
ref class Department
{
private:
int _deptId;
String^ _deptName;
String^ _deptLocation;
public:
Department(int id, String^ deptName, String^ deptLocation);
<...snip...>
}
cstr和set函数:
Department::Department(int id, String^ deptName, String^ deptLocation)
{
setDeptId(id);
setDeptName(deptName);
setDeptLocation(deptLocation);
}
void Department::setDeptId(int id)
{
_deptId = id;
}
void Department::setDeptName(String^ name)
{
_deptName = name;
}
void Department::setDeptLocation(String^ location)
{
_deptLocation = location;
}
很抱歉,如果这是一个简单的错误或愚蠢的问题,但我是CLI的新手。
答案 0 :(得分:0)
Department^ temp(deptId, Name, Location);
显然应该是:
Department^ temp(gcnew Department(deptId, Name, Location));
虽然我不知道为什么。洛尔