c++类对象的例子(非标准语法;请使用 “&“ 来创建指向成员的指针,如何解决?)

64 篇文章 0 订阅
17 篇文章 0 订阅

非标准语法;请使用 “&” 来创建指向成员的指针,如何解决?

在main.cpp中有问题

Game.cpp......

#include "Game.h"
#include<iostream>
#include<string>
#include<time.h>
#include<stdlib.h>
#include<new>
#include<typeinfo>
#include<algorithm>
Game::Game()
{
	cout << "begin to no paraments initialize......\n";
}

Game::Game(const string &n,const string &u,const string &g,const int &m)
{
	cout << "begin to initialize......\n";
	this->name = n;
	this->user = u;
	this->gun.push_back(g);
	this->money = m;
}

Game::~Game()
{
	cout << "begin to deconstruct......\n";
}

void Game::add_money_base(int in_m)
{
	cout << "welcome to recharge money!!!\n";
	this->money += in_m;
	cout << "your money is: " << this->money << endl;
}

void Game::add_gun_base(const string &g)
{
	cout<<"you has added: "<<g<<" in your gun"<<endl;
	gun.push_back(g);
	cout << "your guns are: ";
	for (vector<string>::iterator i = gun.begin();i != gun.end();i++)
	{
		cout << *i <<" ";
	}
	cout << endl;
}

void Game::show_gun_base()
{
	for (vector<string>::iterator i = gun.begin(); i != gun.end(); i++)
	{
		cout << "your guns are: \n";
		cout << *i << endl;
	}
}

string & Game::show_game_name_base()
{
	return name;
}

string & Game::show_user_name_base()
{
	return user;
}

void Game::play_game()
{
	cout << "you are playing: " << name << endl;
}

void Game::custom_func_play1()
{
	cout << "you are playing games......\n";
}

void Game::custom_func_play2()
{
	cout << "you are playing iQOO7......\n";
}
Game1::Game1()
{
	cout << "begin to no paraments construct......\n";
}

Game1::Game1(const string &n, const string &u, 
	const string &g, const int &m,const string &p, const string &game_name):Game(n, u, g, m)
{
	this->phone_name = p;
	this->game_name = game_name;
}

Game1::~Game1()
{
	cout << "begin to deconstruct" << endl;
}

void Game1::add_car_son(const string &c)
{
	car.push_back(c);
	cout << "you has added car !!!\n"<<"your cars are : ";
	for_each(car.begin(), car.end(), [](string &s) {cout << s <<" "; });
	cout << endl;
}

void Game1::play_game()
{
	cout << "you are using : "<<phone_name<<
		" to play -> " <<game_name << endl;
}

void Game1::show_message_son(string *f_show_name(), string *f_show_user(), 
	int *f_show_money(), void *show_gun_base())
{
	cout << "your name is: " << *f_show_user() << endl;
	cout << "your game name is: " << *f_show_name() << endl;
	cout << "your money is: " << *f_show_money() << endl;
	show_gun_base();
}

void Game1::show(void *add_money_base(int in_m), void *add_gun_base(const string &g), void *show_gun_base())
{
	add_gun_base("12234");
	add_money_base(999);
	show_gun_base();
}

void Game1::custom_func_play1()
{
	cout << "you are playing games......\n";
}

void Game1::custom_func_play2()
{
	cout << "you are playing iQOO7......\n";
}

Game.h .......
#pragma once
#ifndef GAME_H_
#define GAME_H_

#include<string>
#include<vector>
#include<list>
using namespace std;

class Game
{
private:
	string name;
	string user;
	int money;
	vector<string>gun;

public:
	Game();
	Game(const string &n, const string &u, const string &g, const int &m);
	~Game();
	void add_money_base(int in_m);
	void add_gun_base(const string &g);
	void show_gun_base();
	string &show_game_name_base();
	string &show_user_name_base();
	virtual void play_game();
	virtual void custom_func_play1() = 0;
	virtual void custom_func_play2() = 0;
};

class Game1:public Game 
{
private:
	list<string> car;
	string phone_name;
	string game_name;

public:
	Game1();
	Game1(const string &n, const string &u, const string &g, 
		const int &m, const string &p,const string &game_name);
	~Game1();
	void add_car_son(const string &c) ;
	virtual void play_game();
	void show_message_son(string *f_show_name(), string *f_show_user(),
		int *f_show_money(), void *show_gun_base());
	void show(void *add_money_base(int in_m),
		void *add_gun_base(const string &g),void *show_gun_base());
	virtual void custom_func_play1();
	virtual void custom_func_play2();

};
#endif

main.cpp  .......
#include <iostream>
#include<map>
#include<string>
#include<vector>
#include<time.h>
#include<stdlib.h>
#include<new>
#include<typeinfo>
#include "Game.h"

using namespace std;
	
int main()
{
	Game1 hpjy;
	hpjy.add_gun_base("98k");
	hpjy.add_money_base(10);
	hpjy.add_gun_base("AWM");
	hpjy.add_money_base(10);
	hpjy.add_gun_base("M416");
	hpjy.add_money_base(10);
	cout << endl;
	/*
	Game peace_elite("zjl", "peace elite", "AWM", 0);
	peace_elite.show_gun_base();
	cout << "your game name is: " << peace_elite.show_game_name_base() << endl;
	cout << "your name is: " << peace_elite.show_user_name_base() << endl;
	peace_elite.add_money_base(666);
	cout << endl;
	*/
	Game1 kybc;
	kybc.add_car_son("BMW");
	kybc.add_money_base(10);
	kybc.add_car_son("audi");
	kybc.add_money_base(10);
	kybc.add_car_son("iQOO");
	kybc.add_money_base(10);

	Game *g = new Game1("peace_elite", "xwa", "M416", 777, "iQOO", "peace_elite");
	g->play_game();
	cout << g->show_game_name_base() << endl;

	Game1 iQOO("peace_elite", "xwa", "M416", 777, "iQOO", "peace_elite");
	iQOO.custom_func_play1();
	iQOO.custom_func_play2();
	
	//下面两处有问题:非标准语法;请使用 "&" 来创建指向成员的指针,如何解决?
	//iQOO.show_message_son(&Game::show_user_name_base,iQOO.show_game_name_base,iQOO.add_money_base,iQOO.show_gun_base);
	//iQOO.show(&Game::add_money_base, iQOO.add_gun_base, iQOO.show_gun_base);

	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值