【问题标题】:C++ error trying to return pointer that points to dynamic array in Visual Studio尝试返回指向 Visual Studio 中动态数组的指针的 C++ 错误
【发布时间】: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


【解决方案1】:

您的类定义末尾缺少;,以及您稍后定义的任何成员函数。

您的 Student.cpp 开头缺少 #include "Student.h"

您的 Student.h 一开始就缺少几个标准的#includes。

这些表达式stu.getFirstName 可能应该类似于stu.getFirstName()

拿起一本介绍 C++ 的书。

【讨论】:

  • Student 声明没有 OP 稍后定义的任何成员函数怎么样?
  • @SergeyA:确实,这肯定没有帮助。
猜你喜欢
  • 2021-10-16
  • 2018-07-29
  • 1970-01-01
  • 2015-03-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-07
相关资源
最近更新 更多