我正在尝试在构造函数中使用setter函数,这是我以前从未做过的事情。这给了我以下错误:
[Error] no match for call to '(Laptop) (const char [5], const char [3], int, int)'
我还需要编写setter函数吗?我的意思是在构造函数之外?我的意思是这样
void Laptop::setBrand(char a[])
{
brand=a;}
我认为错误在于第二个构造函数,该构造函数带有四个参数,但我找不到它。
代码如下:
#include <iostream>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
using namespace std;
class Laptop{
private:
char brand[10];
char processor[10];
int ram;
int hardDrive;
public:
void setBrand(char Bra[]);
void setProcessor(char Pro[]);
void setRam(int Ram);
void setHardDrive(int HDrive);
char *getBrand();
char *getProcessor();
int getRam();
int getHardDrive();
Laptop();
Laptop(char [],char [],int ,int );
};
Laptop::Laptop(){
cout<<"Default constructor called...\n";
strcpy(brand,"None");
strcpy(processor,"None);
ram=0;
hardDrive=0;
}
我认为错误是在构造函数中
Laptop::Laptop(char Bra[],char Pro[],int Ram,int HDrive)
{
cout<<"Parameterized constructor called...\n";
setBrand(Bra );
setProcessor(Pro );
setRam(Ram);
setHardDrive(HDrive);
}
char *Laptop::getBrand()
{
return brand;
}
char *Laptop::getProcessor()
{
return processor;
}
int Laptop::getRam()
{
return ram;
}
int Laptop::getHardDrive()
{
return hardDrive;
}
int main()
{
Laptop laptopObj1;
Laptop laptopobj1("Dell","i5",4,500);
cout<<"Brand :"<<laptopObj1.getBrand()<<"\n";
cout<<"Processor :"<<laptopObj1.getProcessor()<<"\n";
cout<<"Ram :"<<laptopObj1.getRam()<<"\n";
cout<<"HardDrive :"<<laptopObj1.getHardDrive()<<"\n";
cout<<"Brand :"<<laptopObj2.getBrand()<<"\n";
cout<<"Processor :"<<laptopObj2.getProcessor()<<"\n";
cout<<"Ram :"<<laptopObj2.getRam()<<"\n";
cout<<"HardDrive :"<<laptopObj2.getHardDrive()<<"\n";
}
答案 0 :(得分:3)
您要传递两个const char*
作为参数,因此您应该更改此参数:
Laptop::Laptop(char Bra[],char Pro[],int Ram,int HDrive)
对此:
Laptop::Laptop(const char* ,const char* ,int Ram,int HDrive)
答案 1 :(得分:3)
您在这里有很多错误。
populate_sdk
您不能两次使用构造函数。您在第一行中对两个对象都使用了构造函数Laptop laptopObj1,laptopObj2;
...
laptopObj2("Dell","i5", 4, 500);
,然后尝试对Laptop()
使用第二个构造函数。
您可以将第二行更改为:
laptopObj2
或者甚至可以在此处定义它:
laptopObj2 = Laptop("Dell","i5", 4, 500);
Laptop laptopObj1;
....
Laptop laptopObj2("Dell","i5", 4, 500);
构造函数定义中还有另一个问题:
Laptop()
还有一个问题:您对该类的某些功能没有定义:
Laptop::Laptop(char Bra[],char Pro[],int Ram,int HDrive)
{
.....
setBrand(Bra []); // Remove the []
setProcessor(Pro []); // Remove the []
....
}
编辑:
c ++的对象之一是void setBrand(char Bra[]);
void setProcessor(char Pro[]);
void setRam(int Ram);
void setHardDrive(int HDrive);
void display();
(std::string
)。您可以使用它代替#include <string>
,这是在c ++中声明字符串的更简单方法。例如,在您的情况下:
char str[]
答案 2 :(得分:0)
您确实应该使用std :: string而不是char []。
关于构造函数的问题,不需要调用setter,如以下代码所示。 C ++具有该功能的快捷方式。我已经使用过std :: string并修复了所有错字。
#include <iostream>
#include <string>
using namespace std;
class Laptop
{
private:
string brand;
string processor;
int ram;
int hardDrive;
public:
void setBrand(string Bra);
void setProcessor(string Pro);
void setRam(int Ram);
void setHardDrive(int HDrive);
string getBrand();
string getProcessor();
int getRam();
int getHardDrive();
Laptop();
Laptop(string, string, int, int);
};
Laptop::Laptop()
: brand("None")
, processor("None")
, ram(0)
, hardDrive(0)
{
cout << "Default constructor called...\n";
}
Laptop::Laptop(string Bra, string Pro, int Ram, int HDrive)
: brand(Bra)
, processor(Pro)
, ram(Ram)
, hardDrive(HDrive)
{
cout << "Parameterized constructor called...\n";
}
string Laptop::getBrand()
{
return brand;
}
string Laptop::getProcessor()
{
return processor;
}
int Laptop::getRam()
{
return ram;
}
int Laptop::getHardDrive()
{
return hardDrive;
}
int main()
{
Laptop laptopObj1;
Laptop laptopObj2("Dell", "i5", 4, 500);
cout << "Brand :" << laptopObj1.getBrand() << "\n";
cout << "Processor :" << laptopObj1.getProcessor() << "\n";
cout << "Ram :" << laptopObj1.getRam() << "\n";
cout << "HardDrive :" << laptopObj1.getHardDrive() << "\n";
cout << "Brand :" << laptopObj2.getBrand() << "\n";
cout << "Processor :" << laptopObj2.getProcessor() << "\n";
cout << "Ram :" << laptopObj2.getRam() << "\n";
cout << "HardDrive :" << laptopObj2.getHardDrive() << "\n";
}
迈克