C ++ Visual Studio“非标准语法";使用“&"创建指向“成员"的指针 [英] C++ Visual Studio "Non-standard syntax; use '&' to create a pointer to member"
问题描述
我多次遇到此错误(错误C3867:非标准语法;使用&"创建指向成员的指针).我知道这个问题已经被问过很多次了,但我不知道为什么会发生此问题以及如何解决该问题.我已经阅读了很多指南,指针如何工作,并且尝试了新知识,但是我不知道如何正确地做.
I have a ran in to this error (error C3867: non-standard syntax; use '&' to create a pointer to member) a couple of times. I know this question has been asked a lot of times, but I don't get why the problem happens and what I can do to fix it. I've read a lot of guides how pointers work and I've tried to play with the new knowledge, but I don't know how to do it correctly.
对于这个问题,我编写了一个简单的代码.有人可以帮助我了解为什么会发生此错误以及如何解决此代码吗?
For this question I have made a simple code. Can someone help me understand why this error occurs and how to fix this code?
错误:错误C3867:"BankAccount :: amountOfMoney":非标准语法;使用&"创建指向成员的指针
Error: error C3867: 'BankAccount::amountOfMoney': non-standard syntax; use '&' to create a pointer to member
Source.cpp
#include <iostream>
#include <string>
#include "BankAccount.h"
using namespace std;
int main(){
BankAccount bankAccount1("testName", 200.0);
cout << bankAccount1.amountOfMoney << endl;
}
BankAccount.h
#pragma once
#include <string>
using namespace std;
class BankAccount
{
public:
BankAccount();
BankAccount(string name, double money);
~BankAccount();
double amountOfMoney();
private:
string name;
double money;
};
BankAccount.cpp
#include "BankAccount.h"
BankAccount::BankAccount()
{
}
BankAccount::BankAccount(string n, double m) {
name = n;
}
BankAccount::~BankAccount()
{
}
double BankAccount::amountOfMoney() {
return money;
}
推荐答案
您忘记了函数调用运算符()
.将您的main
代码更改为:
You forgot the function call operator ()
. Change your main
code to:
int main(){
BankAccount bankAccount1("testName", 200.0);
cout << bankAccount1.amountOfMoney() << endl;
}
在没有括号的情况下,它尝试打印成员函数的地址,除非该函数不是类的成员,否则它无法执行此操作.
Without the parentheses it tries to print the address of a member function, which it is not able to do unless the function is not a member of a class.
这篇关于C ++ Visual Studio“非标准语法";使用“&"创建指向“成员"的指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!