【问题标题】:Set and get methods in a class for objects在类中设置和获取对象的方法
【发布时间】:2020-04-16 06:29:26
【问题描述】:

我有一个 Result 类,它将另外两个类 UNIT 和 Date 作为对象参数,以便我可以将值存储在其中以用于这些类。我会记下我到目前为止所得到的,我得到的一些错误如下。

error: prototype for 'int Result::GetUnit() const' does not match any in class 'Result'|
lab2\Result.h|17|error: candidate is: Result Result::GetUnit() const|
lab2\Result.cpp|66|error: prototype for 'int Result::GetDate()' does not match any in class 'Result'|
lab2\Result.h|18|error: candidate is: Result Result::GetDate() const|
lab2\Result.cpp|73|error: 'SetResult' was not declared in this scope|
error: 'SetResult' was not declared in this scope|

对于错误not declared in this scope,经过研究我了解到我需要在第一次调用之前定义函数。我已经在 .cpp 文件中完成了该操作,但仍然出现错误。我做错了什么?

结果.h:

#ifndef RESULT_H
#define RESULT_H

#include <iostream>
#include <string>
#include "UNIT.h"
#include "Date.h"


//const unsigned ResultSize = 10;
using namespace std;
class Result
{
    public:
        Result(){};
        Result(UNIT unitobj1, unsigned marks1, Date dateobj1);
        Result GetUnit() const;
        Result GetDate() const;
        void SetDate(Date dateobj1);
        void SetUnit(UNIT unitonj1);
        void SetMarks( unsigned marks1 );
        unsigned GetMarks() const;

        void SetCredits( unsigned cred );
        unsigned GetCredits() const;

        string GetID() const;
        void SetID(string idd);


        void SetResult(istream & input);
        void GetResult(ostream & os);//unsigned GetUnit() const;


    private:

        UNIT unitobj;
        Date dateobj;
        string id;
        int credits;
        unsigned marks;

};
inline unsigned Result::GetCredits() const
{
  return credits;
}

ostream & operator <<( ostream & os, const Result & S);
istream & operator >>( istream & input, Result & S);

#endif // RESULT_H

结果.cpp:

#include "Result.h"
#include "UNIT.h"
#include "Date.h"

Result::Result(UNIT unitobj1, unsigned marks1, Date dateobj1)
{

    unitobj = unitobj1;
    marks = marks1;
    dateobj = dateobj1;

}

void Result::SetResult(istream &input){

    UNIT unitobj1;
    unsigned marks1;
    Date date1;

    input >> unitobj1 >> marks1 >> date1;

    SetUnit(unitobj1);
    SetMarks(marks1);
    SetDate(date1);

}



void Result::GetResult(ostream &os){

    os << GetUnit() < "  Marks: " << GetMarks() << '\n' << GetDate() << '\n';

}

void Result::SetUnit(UNIT unitobj1){

    unitobj = unitobj1;

}

void Result::SetMarks(unsigned marks1){

    marks = marks1;

}

void Result::SetDate(Date dateobj1){

    dateobj = dateobj1;

}

Result::GetUnit() const{

    return unitobj;

}

inline unsigned Result::GetMarks() const{

    return marks;

}

Result::GetDate(){

    return dateobj;

}
istream & operator >>( istream & input, Result & S)
{
      SetResult(input);
      return input;

}

ostream & operator <<( ostream & os, const Result & S)
{
      GetResult(os);
      return os;
}

日期.h:

#if !defined(_DATE_H)
#define _DATE_H

#include <iostream>
#include <string>

using namespace std;

class Date {
public:
    Date();
    Date(unsigned day1, string month1, unsigned year1);

    void SetDay(unsigned day1);
    void SetMonth(string month1);
    void SetYear(unsigned year1);
    unsigned GetDay() const;
    string GetMonth() const;
    unsigned GetYear() const;

    void SetDate(istream &input);
    void GetDate(ostream & os);


private:

    unsigned day;
    string month;
    unsigned year;

};

 ostream & operator <<(ostream & os, const Date & D);
 istream & operator >>(istream & input, Date & D);
#endif  //_DATE_H

日期.cpp:

//
//
//  Generated by StarUML(tm) C++ Add-In
#include "Date.h"

Date::Date(unsigned day1, string month1, unsigned year1) {

    day = day1;
    month = month1;
    year = year1;

}


void Date::SetDay(unsigned day1) {

    day = day1;

}

void Date::SetMonth(string month1) {

    month = month1;

}

void Date::SetYear(unsigned year1) {

    year = year1;

}

inline unsigned Date::GetDay() const {

    return day;

}

string Date::GetMonth() const {

    return month;

}

inline unsigned Date::GetYear() const {

    return year;

}
void Date::SetDate(istream &input){

    unsigned day1;
    string month1;
    unsigned year1;


    input >> day1 >> month1 >> year1;
    SetDay(day1);
    SetMonth(month1);
    SetYear(year1);

}

void Date::GetDate(ostream &os){

    os << "  Date: " << GetDay() << " " << GetMonth() << " " << GetYear();

}


istream & operator >>( istream & input, Date & D) {

    SetDate(input);
    return input;

}

ostream & operator <<( ostream & os, const Date & D) {

    GetDate(os);

    return os;

}

UNIT.h:

        #ifndef UNIT_H
        #define UNIT_H

        #include <iostream>
        #include <string>  // C string library

        using namespace std;



        const unsigned UnitNameSize = 10;

        class UNIT
        {
        public:
          UNIT();
          UNIT( string nam, string idd, unsigned cred);

          void SetName(string nam);
          string GetName() const;

          void SetMarks(unsigned marks1);
          unsigned GetMarks();

          void SetCredits(unsigned cred);
          unsigned GetCredits() const;

          string GetID() const;
          void SetID(string idd);

          void SetUnit(istream & input);
          void GetUnit(ostream & os);

        private:


          string name;  
          string id;   
          unsigned marks;
          int credits;
        };

        ostream & operator <<( ostream & os, const UNIT & U);
        istream & operator >>( istream & input, UNIT & U);



        #endif // UNIT_H

UNIT.cpp:

#include "UNIT.h"
#include <string>

UNIT::UNIT()
{
  name[0] = '\0';
}

void UNIT::SetName(string nam)
{
  name = nam;
}


string UNIT::GetName() const
{
  return name;
}

void UNIT::SetID(string idd)
{
  id = idd;
}


string UNIT::GetID() const
{
  return id;
}

void UNIT::SetCredits(unsigned cred){

    credits = cred;

}

inline unsigned UNIT::GetCredits() const{

    return credits;

}
UNIT::UNIT( string nam, string idd,
                unsigned cred)
{
  name.replace(0, 10, nam );
  id = idd;
  credits = cred;
}

void UNIT::SetUnit(istream &input){

  string nam;
  string idd;
  unsigned cred;

  getline(input,nam, '\n');
  getline(input,idd,'\n');
  input >> cred;

  SetName(nam);
  SetID(idd);
  SetCredits(cred);

}

void UNIT::GetUnit(ostream & os){

  os << "  Unit ID:  " << GetID() << '\n'
     << "  Unit Name: " << GetName() << '\n'
     << "  Credits: " << GetCredits() << '\n';

}


istream & operator >>( istream & input, UNIT & U)
{
  SetUnit(input);

  return input;
}

ostream & operator <<( ostream & os, const UNIT & U)
{
  GetUnit(os);
  return os;
}

注意:我知道我可以只创建类的重载运算符方法并将它们声明为朋友,但我试图通过使用 set 和 get 方法来实现这一点。任何帮助将不胜感激!

【问题讨论】:

    标签: c++ class get set


    【解决方案1】:

    所以,这很简单,看看Result::GetUnit

    Result::GetUnit() const {
        return unitobj;
    }
    

    此方法缺少返回类型。现在看unitobj

    UNIT unitobj;
    

    所以很明显返回类型应该是UNIT,所以上面的方法应该定义为

    UINT Result::GetUnit() const {
        return unitobj;
    }
    

    现在看看你是如何在你的类中声明这个方法的

    class Result
    {
        ...
        Result GetUnit() const;
    

    这里你给它一个返回类型Result,我们已经看到返回类型应该是UINT,所以把上面的改成

    class Result
    {
        ...
        UNIT GetUnit() const;
    

    Result::GetDate 也有类似的问题,这里的返回类型应该是Date,但你再次指定它为Result

    对于SetResult 错误,您只需指定您正在对Result 对象调用SetResult 方法。编译器认为您正在调用全局 SetResult 函数(不存在)

    这样

    istream & operator >>( istream & input, Result & S)
    {
          S.SetResult(input);
          return input;
    }
    

    S.SetResult(input); 告诉编译器您要使用S 引用的Result 对象调用SetResult 方法。

    您可以通过仔细阅读错误消息来判断这一点,它说的是'SetResult' was not declared in this scope,而不是'Result::SetResult' was not declared in this scope。正如你所说,你已经声明了Result::SetResult,但你的代码没有调用它,它试图调用另一个名为SetResult 的函数,编译器正确报告没有声明这样的函数。

    【讨论】:

    • 这很有意义!谢谢,在将其更改为您所说的要求后,这些错误就消失了。现在在函数 GetResult 中向我显示了最后一个错误。它将“const char [10]”和“unsigned int”类型的无效操作数表示为二进制“运算符”
    • 这只是一个错字,void Result::GetResult(ostream &amp;os){ os &lt;&lt; GetUnit() &lt; " Marks: " &lt;&lt; GetMarks() &lt;&lt; '\n' &lt;&lt; GetDate() &lt;&lt; '\n'; } 应该是void Result::GetResult(ostream &amp;os){ os &lt;&lt; GetUnit() &lt;&lt; " Marks: " &lt;&lt; GetMarks() &lt;&lt; '\n' &lt;&lt; GetDate() &lt;&lt; '\n'; },即&lt;&lt; 而不是&lt;。错误信息中的const char[10]指的是" Marks: "
    • 傻我。忍受我约翰,因为在修复它之后,我现在在我的 Date 类中出现了一些非常奇怪的错误。我所有的函数定义都有这里首先定义的 Date::Function' 的错误多重定义,它指向完全相同的行。当我将所有内容都内联时,我的 Result 类中出现错误,当我在头文件中将其定义为免费方法时已经,使用 Result 作为对象参数而不是 Date。
    • date.cpp 中的一些函数已定义为 inline。那是一个错误。如果它在头文件中定义,则始终使其成为inline,如果它在cpp文件中定义,则永远不会使其成为inline
    • 这里的另一个错误,这个UNIT::UNIT() { name[0] = '\0'; }应该只是UNIT::UNIT() { }你不需要nul终止C++字符串。而且由于您的字符串长度为零,name[0] = '\0'; 在技术上是一个错误,尽管如果它真的引起任何问题我会感到惊讶。
    猜你喜欢
    • 2015-04-09
    • 2021-10-14
    • 1970-01-01
    • 2011-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多