0

I'm having a tough time creating a an array that holds objects of a class I made. I cannot use std::vector to hold the objects so here is what I tried to do:

This is my cpp file: Resistor.cpp:

#include "Resistor.h"   
Resistor::Resistor(int rIndex_, string name_, double resistance_){
    int rIndex = rIndex_;
    name = name_;
    resistance = resistance_;       
}

I'm trying to make an array of these Resistor objects in another cpp file: Rparser.cpp:

#include "Resistor.h"
#include "Node.h"
#include "Rparser.h"

Rparser::Rparser(int maxNodes_, int maxResistors_){
    maxNodes = maxNodes_;
    maxResistors = maxResistors_;
    resistorArray = new Resistor[maxResistors_];   //trying to make an array   
}

My Rparser.h file looks like this, as you can see I declared a pointer that points to Resistor datatype:

#include "Resistor.h"
#include "Node.h"
class Rparser{
public:
    int maxNodes;
    int maxResistors;
    Resistor *resistorArray;  //declared a pointer here

    Rparser(int maxNodes_, int maxResistors_);
    ~Rparser(){};

I'm getting the following errors:

error: no matching function for call to ‘Resistor::Resistor()’
note: candidates are: Resistor::Resistor(int, std::string, double, int*)
note:                 Resistor::Resistor(const Resistor&)

Why is it treating the line resistorArray = new Resistor[maxResistors] as a function call instead of creating an array?

1
  • 3
    Because there are (multiple) function calls: The constructors. As long as you don't have a constructor without parameter, you can't create objects without parameters. [This isn't Java. An array is not "n places where are object could be, but initially empty" but "n objects"] Commented Oct 12, 2015 at 0:03

1 Answer 1

2

You need a Resistor constructor that takes no arguments (that is what the compiler is telling you in the error message, it is not a function call, but a call to the no arguments constructor.). Think about it, new Resistor[] specifies no constructor arguments.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.