gpt4 book ai didi

c++ - 模板 C++ 错误 : could not deduce template argument

转载 作者:行者123 更新时间:2023-11-30 01:00:54 26 4
gpt4 key购买 nike

我正在尝试添加一个函数模板,如果它包含精度值或阀门和值,它将打印。除此功能外,程序的其余部分均有效。我不确定我做错了什么,但我收到的错误是:

错误 C2784:“void printInstrumentDetail(const I *const )”:无法从“std::vector<_Ty>”中推断出“const I *const”的模板参数

#include <iostream>
#include <vector>
#include <iomanip>
#include <string>
#include "Instruments.h"
#include "Brass.h"
#include "Strings.h"

using namespace std;

//template<typename I> <---Problem
//void printInstrumentDetail(const I * const a)
//{
// for (size_t i = 0; i < 6; i ++)
// {
// cout << "The details for " << a[i]->getName()
// << ": " << a[i]->print();
// }
//}
int main()
{
double total = 0;

Strings violin("Violin", 553.90, 3);
Strings cello("Cello", 876.45, 3);
Strings viola("Viola", 200.50, 23);
Brass tuba("Tuba", 1400.10, 1.23);
Brass trumpet("Trumpet", 500.00, 4.32);
Brass sax("Sax", 674.78, .99);

vector <Instruments *> band(6);

band[0] = &violin;
band[1] = &tuba;
band[2] = &cello;
band[3] = &trumpet;
band[4] = &viola;
band[5] = &sax;

cout << fixed << setprecision(2);

cout << "The instruments in the band are:\n";
//Get name and cost of each
for (size_t i = 0; i < 6; i ++)
{
cout << band[i]->getName() << " $"
<< band[i]->getCost() << endl;
}

cout << "\nThen band is warming up..." << endl;
//Get descrition of how sound is made of each
for (size_t i = 0; i < 6; i ++)
{
cout << "This " << band[i]->getName()
<< " makes sounds by " ;
band[i]->playSound();
}
cout << "\nTotal cost of the band is: $" ;
//Get total cost of all instruments
for (size_t i = 0; i < 6; i ++)
{

total = band[i]->getCost() + total;
}
cout << total << endl;

//printInstrumentDetail(band); <--Problem

return 0;
}

这是基类:

#ifndef INSTRUMENTS_H
#define INSTRUMENTS_H

#include <string>

using namespace std;

class Instruments
{
public:
Instruments(string, double);

void setName(string);
virtual string getName();

void setCost(double);
virtual double getCost();

virtual void print();

virtual void playSound();

private:
string name;
double cost;
};
#endif

#include <iostream>
#include "Instruments.h"

using namespace std;

Instruments::Instruments(string n, double c)
{
name = n;
cost = c;
}
void Instruments::setName(string n)
{
name = n;
}
string Instruments::getName()
{
return name;
}
void Instruments::setCost(double c)
{
cost = c;
}
double Instruments::getCost()
{
return cost;
}
void Instruments::print()
{

}
void Instruments::playSound()
{
//empty
}

派生类低音:

#ifndef BRASS_H
#define BRASS_H

#include <string>
#include "Instruments.h"

using namespace std;

class Brass : public Instruments
{
public:
Brass(string, double, double);

void setPrecisionValue(double);
double getPrecisionValue();
void print() ;
void playSound();

private:
double precision;
string sound;
};
#endif

#include <iostream>
#include "Brass.h"

using namespace std;

Brass::Brass(string n, double c, double p)
:Instruments(n, c)
{
precision = p;
}
void Brass::setPrecisionValue(double p)
{
precision = p;
}
double Brass::getPrecisionValue()
{
return precision;
}
void Brass::print()
{
cout << getPrecisionValue() << endl;
}
void Brass::playSound()
{
cout << "blowing in a mouthpiece." << endl;
Instruments::playSound();
}

派生类字符串:

#ifndef STRINGS_H
#define STRINGS_H

#include <string>
#include "Instruments.h"

using namespace std;

class Strings : public Instruments
{
public:
Strings(string, double, int);

void setValves(int);
int getValves();
void print();
void playSound();

private:
int valves;
};
#endif

#include <iostream>
#include "Strings.h"

using namespace std;

Strings::Strings(string n, double c, int v)
:Instruments(n, c)
{
valves = v;
}
void Strings::setValves(int v)
{
valves = v;
}
int Strings::getValves()
{
return valves;
}
void Strings::print()
{
cout<< getValves() << endl;
}
void Strings::playSound()
{
cout << "striking with a bow." << endl;
Instruments::playSound();
}

最佳答案

嗯,问题是你的模板需要一个指针:

template<typename I>   
void printInstrumentDetail(const I * const a);

但是你给它一个 vector ,而不是一个指针:

vector <Instruments *> band(6);
...
printInstrumentDetail(band);

您可以通过将指针传递给 printInstrumentDetail 函数来解决这个问题,如下所示:

printInstrumentDetail(&band[0]);

但实际上,您最好修改 printInstrumentDetail 以获取一个容器或一对迭代器:

template <typename ContainerT>   
void printInstrumentDetail(const ContainerT& a)

template <typename IteratorT>
void printInstrumentDetail(IteratorT first, IteratorT last)

对函数的定义进行适当的修改。

关于c++ - 模板 C++ 错误 : could not deduce template argument,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1841007/

26 4 0