对象不侦听方法或未创建?(C++OO)
每当我试图创建一个对象并对其调用函数时,它似乎都不起作用 我不知道为什么,因为我似乎也没有错误 我在这里搜索了关于构造函数和toString方法的内容,但没有找到任何有效的方法 我试图在构造函数成员中编辑不同的成员, 试图重写toString方法。 试图创建没有指针的本地对象 但它不会返回调用构造函数时创建的对象中的内容 这个问题在哪里 这是我的密码: .h文件:对象不侦听方法或未创建?(C++OO),c++,C++,每当我试图创建一个对象并对其调用函数时,它似乎都不起作用 我不知道为什么,因为我似乎也没有错误 我在这里搜索了关于构造函数和toString方法的内容,但没有找到任何有效的方法 我试图在构造函数成员中编辑不同的成员, 试图重写toString方法。 试图创建没有指针的本地对象 但它不会返回调用构造函数时创建的对象中的内容 这个问题在哪里 这是我的密码: .h文件: #pragma once #include "stdafx.h" #include <string> #include
#pragma once
#include "stdafx.h"
#include <string>
#include <sstream>
#include <iostream>
using namespace std;
class Store{
private:
int id;
string name;
string adress;
string telephone;
string btwNumber;
public:
int getId();
void setId(int);
string getName();
void setName(string);
string getAdress();
void setAdress(string);
string getTelephone();
void setTelephone(string);
string getBtwNumber();
void setBtwNumber(std::string);
string toString();
Store(int, string, string , string, string);
};
.cpp文件:
// Store.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "Store.h"
Store::Store(int idnum, string nameS, string adreS, string telephonE, string btwnummeR){
idnum = id;
nameS = name;
adreS = adress;
telephonE = telephone;
btwnummeR = btwNumber;
}
int Store::getId()
{
return id;
}
void Store::setId(int id){
this->id = id;
}
string Store::getName(){
return naam;
}
void Store::setName(string name){
this->naam = naam;
}
string Store::getTelephone(){
return telephone;
}
void Store:setTelephone(string telephone){
this->telephone = telephone;
}
string Store::getBtwNumber()
{
return btwNumber;
}
void Store::setBtwNumber(string btwNumber){
btwNumber = btwNumber;
}
string Store::getAdress(){
return adress;
}
void Store::setAdress(string adress){
this->adress = adress;
}
string Store::toString(){
stringstream s;
s << "Id: " << id << endl;
s << "Naam: " << name << endl;
s << "Adres: " << adress << endl;
s << "Telefoonnummer: " << telephone << endl;
s << "BTWnummer: " << btwNumber << endl;
return s.str();
}
int _tmain(int argc, _TCHAR* argv[])
{
Store *test = new Store (4, "Test", "test", "test", "test");
test->toString();
system("Pause");
return 0;
}
toString方法确实有效,但它不会神奇地决定将其返回值输出到屏幕。你必须自己做:
std::cout << test->toString() << std::endl;
您需要在cpp文件的顶部添加include。您的构造函数是反向的:您正在将成员变量分配给构造函数参数,而不是相反
nameS = name;
应该是
name = nameS;
等等那么cout-toString;?我似乎很接近,但它仍然没有给我任何回报,除了boggus的身份证号码?看看@Desu_Never_Lies写了什么。代码中还有一些小的拼写错误,比如使用naam代替名称和a::变成:。这对我很有帮助!非常感谢你。但在课堂练习中,我看到我们的老师在将成员分配到构造函数中时经常使用完全相同的成员。这实际上是如何工作的?编译器如何知道实际成员是什么?