Я пытаюсь научиться использовать функции класса в c++. У меня есть код, который я написал здесь, но что-то не так. Я знаю, что вы не делаете домашнее задание... но если вы можете помочь мне узнать его с использованием таких вещей, как вы смотрели здесь. Я провел исследование..google cpluplus site..but есть что-то, чего я не получаю так, надеюсь, вы можете мне помочь. Я использую 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);
to numbers.initialize(n1, n2);
Кроме этого ваш код в порядке.
addSubtract::initialize(int, int)' 33 C:\Users\ddempsey\Desktop\class\addSubtractMain.cpp undefined reference to
addSubtract :: add ()»
n1
иn2
подint1
иint2
, не так ли?