【发布时间】:2018-03-13 21:36:49
【问题描述】:
在重载流插入运算符时,我在尝试返回指向 C++ 中的动态数组的指针时遇到了一些问题。我正在使用 Visual Studio 2017。这是相关代码。如果您认为有必要,我会发布我的所有代码,但我不想让您阅读整个内容,因为我目前只有这一点有问题。另外,这个任务要求我使用动态数组,所以请不要告诉我只使用 std::vector。
学生.h
class Student
{
private:
string firstname;
string lastname;
unsigned int id;
unsigned int items_checkedout;
string *things = new string[items_checkedout];
}
学生.cpp
const string* Student::getthings()
{
return things;
}
const string Student::getFirstName()
{
return firstname;
}
const string Student::getLastName()
{
return lastname;
}
const int Student::getID()
{
return id;
}
unsigned int Student::CheckoutCount()
{
return items_checkedout;
}
ostream& operator<<(ostream& out, const Student& stu)
{
const string *things = stu.getthings;
out << stu.getID << " " << stu.getFirstName << " " << stu.getLastName <<
endl;
out << stu.CheckoutCount;
if (stu.CheckoutCount > 0)
{
for (int i = 0; i < stu.CheckoutCount; i++)
{
out << things[i];
}
}
}
这是我遇到的错误:
错误 C3867 'Student::getthings':非标准语法;使用 '&' 创建指向成员行 184 的指针
错误 C2276 '*':绑定成员函数表达式第 187 行的非法操作
错误 C3867 'Student::CheckoutCount':非标准语法;使用 '&' 创建指向成员行 188 的指针
“同样的事情,第 189 行
错误 C2296 '>':非法,左操作数的类型为 'unsigned int (__thiscall Student::* )(void) 第 189 行
错误 C2297 '>':非法,右操作数的类型为 'unsigned int(__thiscall 第 189 行
错误 C3867 'Student::CheckoutCount':非标准语法;使用 '&' 创建指向成员行 191 的指针
错误 C2446 '
【问题讨论】:
-
stu.getthings;不是 getthings 函数吗? -
我投票结束这个问题作为离题,因为这个问题已经无法挽救了。
-
如果我可以超越救赎,我不会这样做,但提问者肯定会从 the guided learning of a good book or two 中受益,而不是 Stack Overflow 旨在提供的任何东西。
标签: c++ pointers dynamic-arrays