-3

I am trying to fill a vector that takes multiple types of arguments that I have defined. However, I am getting invalid template argument error despite adding vector library and using namespace std. Is it because I am trying to put the class names I defined? Should I be using another method to merge my different arrays? Here is my relevant header and cpp files. The codes read a netlist file (text file) and should return a vector that contains the component information. Please help.

hpp file:

#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 file:

#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;
}

Thank you!

sdcoders
  • 19
  • 2
  • 2
    `vector` can keep only one type of objects. If you need store in vector multiple types use `std::vector>`, but since you are a beginner this might be to hard to use for you. – Marek R Mar 25 '20 at 18:08
  • Since I am using vector vectorName; synthax I can write this way without variant. Using variant dis not fix the error for me in this case. – sdcoders Mar 25 '20 at 18:24
  • You are not "using vector" since this is ill-formatted (illegal). – Marek R Mar 25 '20 at 18:33

2 Answers2

1

The question is: why do you want to store them together. Are they somehow similar to each other? Do they have some common factor? You haven't provided implementation of your resistor, capacitor and opamp to us. If they are classes with common interface you can consider creating sth like Component and then inherit over it.

#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;
}
rafauu
  • 101
  • 4
-1

The vector can only hold one type, but that type itself may be able to hold other types. You could have a vector of pair or a vector of tuple. Since you have more than two types, use a tuple:

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

Next, what you're trying to do here is totally wrong:

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

Because the vector can only hold one type. If you want to achieve what you're doing as such, you would need to go learn about polymorphism. Otherwise, just insert the items grouped together in a tuple as such:

NetlistReader.push_back(make_tuple(R,C,O));
amr ras
  • 136
  • 10
  • OP wants to be each element to be of either one of three types. If you make a vector of tuples, each element contains an instance of each type – 463035818_is_not_an_ai Mar 25 '20 at 18:41
  • I read `NetlistReader.push_back(R); NetlistReader.push_back(C); NetlistReader.push_back(O);` as an indication that those are 3 distinct element in the vector that are of 3 different types, though, given that the whole question is based on a misunderstanding some further clarification might be necessary – 463035818_is_not_an_ai Mar 25 '20 at 18:46
  • Exactly, the whole question is a misunderstanding. I read those 3 consecutive push_backs as he wants them grouped together. They all seem to be logically related anyways. – amr ras Mar 25 '20 at 18:47