了解重载运算符。收到"non-standard syntax; use '&' to create a pointer to a member"错误
Learning about Overloading Operators. Getting "non-standard syntax; use '&' to create a pointer to a member" error
我正在尝试创建一个使用重载运算符的简单程序。我相信我的那部分是正确的。但是,当我尝试在"Money"类中调用"getDollar"函数时,会出现"非标准语法;使用'&'创建指向成员的指针"错误。我错过了什么?
谢谢你的帮助。(代码张贴在下面)
驱动程序.cpp
#include <iostream>
#include "Money.h"
using namespace std;
// Declaring Variables
Money a;
Money b;
int cents;
int dollars;
// Main Function
int main()
{
cout << "Please enter in a monetary value:n" << "Dollars: ";
cin >> dollars;
cout << "nCents: ";
cin >> cents;
Money a(dollars, cents);
cout << "Please enter in second monetary value:n" << "Dollars: ";
cin >> dollars;
cout << "nCents: ";
cin >> cents;
Money b(dollars, cents);
Money& c = a + b;
cout << "nThe amount of the two added together are " << c.getDollars << "." << c.getCents << endl;
c = a - b;
cout << "nThe amount of the first value subtracted by the second value is " << c.getDollars << "." << c.getCents << endl;
system("PAUSE");
return 0;
}
Money.cpp
#include "Money.h"
#include <iostream>
using namespace std;
Money::Money()
{
dollars = 0;
cents = 0;
}
Money::Money(int d, int c)
{
dollars = d;
cents = c;
}
int Money::getDollars() const
{
return dollars;
}
int Money::getCents() const
{
return cents;
}
Money Money::operator+ (const Money& otherMoney)
{
// Declare a new "Money" object
Money newMoney;
// Add the cents from money object 1 and money object 2
newMoney.cents = cents + otherMoney.cents;
// Add the dollars from money object 1 and money object 2
newMoney.dollars = dollars + otherMoney.dollars;
// Return the new money object
return newMoney;
}
Money Money::operator- (const Money& otherMoney)
{
// Declare a new "Money" object
Money newMoney;
// Subtract the cents of money object to FROM money object 1
newMoney.cents = cents - otherMoney.cents;
// Subtract the dollars of money object to FROM money object 1
newMoney.dollars = dollars - otherMoney.dollars;
// Return the new money object
return newMoney;
}
Money.h
#pragma once
#include <iostream>
using namespace std;
class Money
{
private:
int dollars;
int cents;
public:
// Default Constructor
// Purpose: Remove any data
// Parameter: Void
// Return: None
Money();
// Parameterized Constructor
// Purpose: Set dollars and cents equal to "d" and "c"
// Parameter: two ints, "d" and "c"
// Return: None
Money(int, int);
// getDollars Function
// Purpose: Returns dollars
// Parameter: None
// Return: int (dollars)
int getDollars() const;
// getCents Function
// Purpose: Returns cents
// Parameter: None
// Return: int (cents)
int getCents() const;
// + Operator Overload
// Purpose: Add the money of two different Money Objects together
// Parameter: Money Object
// Return: Money Object
Money operator+ (const Money&);
// - Operator Overload
// Purpose: Subtract the money of one Money object from another
// Parameter: Money Object
// Return: Money Object
Money operator- (const Money&);
};
您正在形成一个指向成员的指针,如下所示:
instance.function
然而,形成指向成员的指针的标准方式是这样的:
&(instance.function)
有些编译器会默默地接受你的方式,但大多数编译器至少会抱怨它,因为它在技术上是不可移植的。
然而,我怀疑你实际上是在试图形成一个指向成员的指针。在这种情况下,您只是忘记了cout语句中函数周围的括号。
要在对象上调用getDollars
函数,需要使用函数调用语法。代替
cout << "nThe amount of the two added together are "
<< c.getDollars << "." << c.getCents << endl;
使用
cout << "nThe amount of the two added together are "
<< c.getDollars() << "." << c.getCents() << endl;
// ^^ ^^
编译器在引导您找到解决方案方面没有太大帮助。它猜测,也许,您想要获得一个指向成员函数的指针,而不是进行函数调用。要获得指向成员函数的指针,您需要使用&Money::getDollars
。
不能使用非常量左值引用来引用临时值。
Money& c
应该是
Money c
相关文章:
- "error: no matching function for call to"构造函数错误
- 为什么创建友元类的实例会导致"undefined reference to"错误?
- 地图'Cannot convert from _Ty2 to T*'错误
- 如何修复 cpp 中的"undefined reference to"错误,当调用刚性车身树中的函数时,如 doKinematics、CreateKinematicCache
- 如何修复 C++ 中的"no matching fuction for call to:"错误
- 在VS代码中使用全局变量时出现"undefined reference to"错误
- C++'Undefined reference to'错误,尽管包含路径中的头文件中存在函数定义
- 为什么我的复制构造函数出现"undefined reference to"错误?
- 无法解决"Undefined reference to ..."错误
- 我收到"no mathing function for call to "错误。给我解决方案
- 几个"undefined reference to"错误
- g++ 的一个奇怪的'undefined reference to'错误
- "is not a member of"和"no matching function for call to"错误
- 将静态 C 库与C++代码链接时出现"undefined reference to"错误
- 头文件中的命名空间出现问题,"undefined reference to"错误?
- G++:"Undefined Reference to"错误
- C++ "undefined reference to"错误
- "Undefined reference to" 错误/静态成员变量 C++/Qt
- 编译器构造 - 当我编译C++代码时"undefined reference to"错误,但原始 makefile 代码编译
- 在C++中使用继承对象时"no matching function for call to"错误