【问题标题】:Invalid template argument in vector / c++vector / c ++中的模板参数无效
【发布时间】:2020-03-25 18:04:41
【问题描述】:

我正在尝试填充一个向量,该向量采用我定义的多种类型的参数。但是,尽管添加了向量库并使用命名空间 std,但我收到了无效的模板参数错误。是因为我试图把我定义的类名?我应该使用另一种方法来合并我的不同数组吗?这是我的相关头文件和 cpp 文件。代码读取网表文件(文本文件)并应返回包含组件信息的向量。请帮忙。

hpp 文件:

#ifndef NETLISTREADER_HPP_
#define NETLISTREADER_HPP_
#include <vector>
#include <iostream>
#include "Resistor.hpp"
#include "Capacitor.hpp"
#include "Voltage.hpp"
#include "Opamp.hpp"

using namespace std;

vector <Resistor, Capacitor, Opamp> NetlistReader;


#endif /* NETLISTREADER_HPP_ */

cpp 文件:

#include <iostream>
#include <string>
#include <fstream>
#include <istream>
#include <cstring>
#include <vector>
#include "Resistor.hpp"
#include "Capacitor.hpp"
#include "Voltage.hpp"
#include "Opamp.hpp"

using namespace std;

vector <Resistor, Capacitor, Opamp> NetlistReader() {

    ifstream netlist("C:\\Users\\lenovo\\Desktop\\BassmanNetlist.txt");
    if (!netlist.is_open())
    {
        std::cerr << "Failed to open netlist.txt." << std::endl;
    }

    int n=0;
    int maxnode=0;
    string str;
    string componentName;
    int node1;
    int node2;
    double value;

    int Rcounter=0;
    int Ccounter=0;
    int Ocounter=0;

    while(getline(netlist,str)){
    netlist >> componentName >> node1 >> node2 >> value;
    maxnode= max(node1,node2);
    n=max(n,maxnode);

    switch(componentName[0]){
    case 'R':
    Rcounter++;
    break;
    case 'C':
    Ccounter++;
    break;
    case 'O':
    Ocounter++;
    break;
    }
    }
    cout<< "n is "<<n<<endl;

    Capacitor C[Ccounter]={};
    Resistor R[Rcounter]={};
    Opamp O[Ocounter]={};

    int counter_R=0;
    int counter_C=0;
    int counter_O=0;

         while(getline(netlist,str)){
            netlist >> componentName >> node1 >> node2 >> value;

            switch(componentName[0]){
            case 'R':
            R[counter_R]=Resistor(node1,node2,value);
            counter_R++;
            break;
            case 'C':
            C[counter_C]=Capacitor(node1,node2,value);
            counter_C++;
            break;
            case 'O':
            O[counter_O]=Opamp(node1,node2,value);
break;
            }
            }
         vector<Resistor,Capacitor, Opamp> NetlistReader;
         NetlistReader.push_back(R);
         NetlistReader.push_back(C);
         NetlistReader.push_back(O);
         return NetlistReader;
}

谢谢!

【问题讨论】:

  • vector 只能保留一种类型的对象。如果您需要在向量中存储多种类型,请使用std::vector&lt;std::variant&lt;list of possible types&gt;&gt;,但由于您是初学者,这对您来说可能很难使用。
  • 因为我使用的是vector vectorName; synthax 我可以这样写,没有变体。在这种情况下,使用变体 dis 不能为我修复错误。
  • 您没有“使用矢量”,因为这是格式错误(非法)。

标签: c++ vector invalid-argument


【解决方案1】:

问题是:为什么要将它们存储在一起。它们在某种程度上彼此相似吗?他们有一些共同的因素吗?你还没有向我们提供你的电阻、电容和运算放大器的实现。如果它们是具有通用接口的类,您可以考虑创建类似 Component 的东西,然后对其进行继承。

#include <iostream>
#include <vector>
#include <memory>

struct IComponent
{
    virtual ~IComponent() = default;
    virtual void someCommonMethod() const = 0;
};

struct Resistor : IComponent
{
    void someCommonMethod() const final
    {
        // impl
    }
};

struct Capacitor : IComponent
{
    void someCommonMethod() const final
    {
        // impl
    }
};

int main()
{
    std::vector<std::unique_ptr<IComponent>> components;;
    components.push_back(std::make_unique<Resistor>());
    components.push_back(std::make_unique<Capacitor>());

    return 0;
}

【讨论】:

    【解决方案2】:

    向量只能容纳一种类型,但该类型本身可能能够容纳其他类型。你可以有一对向​​量 或元组向量。由于您有两种以上的类型,请使用元组:

    vector <tuple<Resistor, Capacitor, Opamp>> NetlistReader;
    

    接下来,你在这里尝试做的是完全错误的:

             vector<Resistor,Capacitor, Opamp> NetlistReader;
             NetlistReader.push_back(R);
             NetlistReader.push_back(C);
             NetlistReader.push_back(O);
    

    因为向量只能容纳一种类型。如果你想实现你正在做的事情,你需要去学习多态性。否则,只需将组合在一起的项目插入一个元组中:

    NetlistReader.push_back(make_tuple(R,C,O));
    

    【讨论】:

    • OP 希望每个元素都属于三种类型之一。如果你制作一个元组向量,每个元素都包含每个类型的一个实例
    • 我读到 NetlistReader.push_back(R); NetlistReader.push_back(C); NetlistReader.push_back(O); 表明这些是向量中的 3 个不同元素,它们属于 3 种不同类型,但是,鉴于整个问题是基于误解,可能需要进一步澄清
    • 没错,整个问题都是一个误解。我读了那 3 个连续的 push_backs,因为他希望它们组合在一起。无论如何,它们似乎在逻辑上都是相关的。
    猜你喜欢
    • 2015-11-27
    • 1970-01-01
    • 2014-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-09
    相关资源
    最近更新 更多