模板C++错误:无法推断模板参数

1 c++

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

错误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;
}
Run Code Online (Sandbox Code Playgroud)

这是基类:

#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
}
Run Code Online (Sandbox Code Playgroud)

派生类Bass:

#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();
}
Run Code Online (Sandbox Code Playgroud)

派生类字符串:

#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();
}
Run Code Online (Sandbox Code Playgroud)

Jam*_*lis 5

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

template<typename I>   
void printInstrumentDetail(const I * const a);
Run Code Online (Sandbox Code Playgroud)

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

vector <Instruments *> band(6);
...
printInstrumentDetail(band);
Run Code Online (Sandbox Code Playgroud)

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

printInstrumentDetail(&band[0]);
Run Code Online (Sandbox Code Playgroud)

但实际上,修改printInstrumentDetail一个容器或一对迭代器你会好得多:

template <typename ContainerT>   
void printInstrumentDetail(const ContainerT& a)
Run Code Online (Sandbox Code Playgroud)

要么

template <typename IteratorT>
void printInstrumentDetail(IteratorT first, IteratorT last)
Run Code Online (Sandbox Code Playgroud)

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