#c
#c
Вопрос:
Я пытаюсь добавить сеттер в свой проект в качестве требования, но застрял на том, чтобы заставить его работать. Я прокомментировал области, которые я должен добавить, но все, что я искал, не сработало. Я знаю, что я близок, но, кажется, все не работает.
#include "pch.h"
#include <string>
#include <conio.h>
#include <iostream>
using namespace std;
template <class T>
class Holder
{
private:
T thing;
int number; //add an integer data member that stores the number of data members of whatever class is stored in "thing"
public:
void standardInput();
void standardOutput();
void setNumber(int); // declare a setter function for the new data member
};
template <class T>
void Holder<T>::standardInput()
{
cout << endl;
cout << "You will be asked to enter " << n.setNumber << " items" << endl; // a line of output that use the data member with the number
cin >> Holder<T>::thing;
}
template <class T>
void Holder<T>::standardOutput()
{
cout << endl;
cout << "Here's the data you requested: " << endl;
cout << Holder<T>::thing << endl;
}
template<class T>
void Holder<T>::setNumber(int n) //implement the setter function for the new data member
{
setNumber = n;
}
// This is the first of two custom classes
class Student
{
friend ostreamamp; operator<<(ostreamamp;, Studentamp;);
friend istreamamp; operator>>(istreamamp;, Studentamp;);
private:
string name;
double tuiton;
};
ostreamamp; operator<<(ostreamamp; out, Studentamp; a)
{
out << "The Student " << a.name << " Tuiton is: " << a.tuiton << endl;
return out;
}
istreamamp; operator>>(istreamamp; in, Studentamp; a)
{
cout << "Enter the name of student: ";
in >> a.name;
cout << "What is the Price of tuiton? ";
in >> a.tuiton;
return in;
}
// This is the second of two custom classes
class FastFood
{
friend ostreamamp; operator<<(ostreamamp;, FastFoodamp;);
friend istreamamp; operator>>(istreamamp;, FastFoodamp;);
private:
int valueNumber;
double cost;
};
ostreamamp; operator<<(ostreamamp; out, FastFoodamp; a)
{
out << " The Combo Number is " << a.valueNumber << " .It costs " << a.cost << endl;
return out;
}
istreamamp; operator>>(istreamamp; in, FastFoodamp; a)
{
cout << "What is the Combo number? ";
in >> a.valueNumber;
cout << "Enter cost: ";
in >> a.cost;
return in;
}
int main()
{
cout << "For an integer: " << endl;
Holder<int> val;
// use the setter to store 1.
val.standardInput();
val.standardOutput();
cout << "For a Student:" << endl;
Holder<Student> aCollegeStudent;
// use the setter to store 2.
aCollegeStudent.standardInput();
aCollegeStudent.standardOutput();
Holder<FastFood> vMeal;
// use the setter to store 3.
vMeal.standardInput();
vMeal.standardOutput();
cout << endl;
system("pause");
return 0;
}
Предполагается, что при этом запрашивается целое число вместе с информацией о студенте и фастфуде. Мне потребовалось добавить установщик в шаблон, но мне не нужно будет использовать getter из-за стандартного ввода и вывода, просто не могу найти правильный способ сделать это.
Комментарии:
1. Пожалуйста, уточните, что вы имеете в виду под
"everything seems to not work"
. В качестве отступления обратите внимание, что вHolder<T>::setNumber
у вас естьsetNumber = n;
—Holder<T>
нет ни одного имени элемента данныхsetNumber
, но у него есть одно имяnumber
.2. Вот в чем я не был уверен, когда сказал, что, похоже, все не работает. Я имел в виду, что все, что я пытался сделать с настройкой сеттера, не сработало. Я опубликовал, как выглядит мой код в настоящее время, я несколько раз менял установщик и закомментировал, куда я должен поместить его для этого задания.
3. Но вам нужно описать симптомы. Компилируется ли это? Если нет, то, пожалуйста, отредактируйте свой вопрос, чтобы включить сообщения об ошибках компиляции.
4. Он компилируется просто отлично, я просто не знаю, как добавить установщик в шаблон функции, и повсюду искал какой-нибудь его пример или какой-либо совет о том, как это сделать.
5. Строка «cout << » Вам будет предложено ввести » << n.setNumber << » items» << endl; » содержит ошибку: «ошибка: ‘n’ не был объявлен в этой области» … вы понимаете, почему?