gpt4 book ai didi

c++ - C++ 的类和 header

转载 作者:行者123 更新时间:2023-11-28 02:51:55 25 4
gpt4 key购买 nike

我正在尝试学习如何在 C++ 中使用类函数。我有我在这里编写的代码,但有些地方不对。我知道你不做作业。但如果你能帮助我学习它没有使用像你在这里看的东西..我已经完成了研究..google cpluplus 网站..但是有些东西我没有得到所以希望你能帮助我。我正在使用 Dev-C++。

代码取两个数字,然后根据选择使用..add..subtract..multiply..and devide 来计算它们

这是我的第一个错误代码

30  23  C:\Users\ddempsey\Desktop\class\addSubtractMain.cpp [Error] no matching function for call to 'addSubtract::initialize()'

这是我的标题代码

#ifndef CLASS_addSubtract_h
#define CLASS_addSubtract_h

class addSubtract
{
private:
int one;
int two;


public:
int add ();
int sub ();
int multi ();
int devide ();
void initialize (int n1, int n2);
};

using namespace std;

#include "addSubtract.h"



void addSubtract::initialize(int n1, int n2)
{
one = n1;
two = n2;
}
int addSubtract::add()
{

return(one + two);
}
int addSubtract::sub()
{

return(one - two);
}

int addSubtract::multi()
{

return(one * two);
}

int addSubtract::devide()
{

return(one / two);
}
//class function code

主要

#include "addSubtract.h"
#include <iostream>
#include <fstream>
#include <string.h>
#include <stdio.h>
using namespace std;
int main(void)
{
addSubtract numbers; //instantiate an object
int n1;
int n2;
char choice;

cout<<"Enter first number to calculate ";
cin>>n1;
cout<<"\n";
cout<<"Enter second number to calculate ";
cin>>n2;
cout<<"\n";
cout<<"what would you like to do\n ";
cout<<" add enter (+)\n ";
cout<<" subtract enter (-)\n ";
cout<<" multiply enter (*)\n ";
cout<<" devide enter (/)\n ";
cin>>choice;

switch (choice)
{
case '+' :
numbers.initialize(int1, int2);
cout<<n1<<" + "<<n2<<" ="<<numbers.add()<<endl;
break;
case '-' :
numbers.initialize(int1, int2);
cout<<n1<<" - "<<n2<<" = "<<numbers.sub()<<endl;
break;
case '*' :
numbers.initialize(int1, int2);
cout<<n1<<" * "<<n2<<" = "<<numbers.multi()<<endl;
break;
case '/' :
numbers.initialize(int1, int2);
cout<<n1<<" / "<<n2<<" = "<<numbers.devide()<<endl;
break;
default:
cout<<"invalid choice" <<endl;
}


system("PAUSE");
return 0;

}

最佳答案

numbers.initialize(int1, int2);numbers.initialize(n1, n2);

除此之外你的代码没问题。

关于c++ - C++ 的类和 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22844972/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com