1

I am trying to initialize my MedList but it's not working. Here's what I'm talking about: repository.h

#include "../domain/farmacy.h"
#include "../utils/DynamicVector.h"
class Repository{

private:
    DynamicVector<Medicine>* MedList; //I made it pointer so it can be dynamic

public:
Repository(); //constructor

repository.cpp

#include "../domain/farmacy.h"
#include "repository.h"
#include "../utils/DynamicVector.h"
#include <stdlib.h>

Repository::Repository(){
    this->MedList=new DynamicVector<Medicine>::DynamicVector(); //error
}

DynamicVector.h

template <typename Element> //this is the Dynamic Vector constructor
DynamicVector<Element>::DynamicVector()
{
    this->cap=10;
    this->len=0;
    this->elems=new Element[this->cap];
}

the error above is:

Multiple markers at this line
    - no match for 'operator=' in '((Repository*)this)->Repository::MedList = (int*)operator 
     new(4u)'
    - expected type-specifier
    - candidate is:
    - expected ';'

this is the medicine class

class Medicine{

private:
    int ID;
    std::string nume;
    double concentratie;
    int cantitate;

The Dynamic Vector class:

template <typename Element>
class DynamicVector{

private:
    Element* elems;
    int cap;
    int len;
    void resize();
    void CopyToThis(const DynamicVector& v);

public:
    DynamicVector(); //constructor implicit
    DynamicVector(const DynamicVector& ); //constructor de copiere
    DynamicVector& operator=(const DynamicVector& );
    ~DynamicVector();
    void addElement(Element elem);
    Element delElementAtPosition(int pos);
    Element getElementAtPosition(int pos);
    int getLen();

};

What am I doing wrong? I tried a lot of variants but nothing seems to work. Could you help me?

Cucerzan Rares
  • 73
  • 3
  • 5
  • 10

3 Answers3

1

I think you're confusing c++ syntax for creating object with some other language, e.g. Java or C#.

In c++, a constructor is called simply by declaring the variable:

DynamicVector<Element> medList; // Calls DynamicVector<Element>::DynamicVector()

The new operator in C#, is to dynamically allocate space for a variable, and returns a pointer to the allocated space. To use it here, you'd have to declare Repository::MedList as a pointer type, and initialize it like so:

DynamicVector<Medicine>* MedList; // in repository.h

this->MedList = new DynamicVector<Medicine>(); // in repository.cpp

However, as Andy Prowl pointed out, it is much better to just let the compiler do the memory management for you. To do so, you should completely remove the erroneous line in repository.cpp. Why? Well, when the repository is constructed, the compiler also tries to construct all member objects using their default constructors. This is exactly what you want, so there is no reason to try to alter the compiler's behavior.

Tomas Aschan
  • 58,548
  • 56
  • 243
  • 402
  • The spirit of this answer is correct, but the OP _has_ declared the `medList` member as a pointer. That was _probably_ unnecessary, but depends on the implementation of `DynamicVector`. – Chad Mar 31 '13 at 12:30
  • DynamicVector* MedList; // in repository.h this->MedList = new DynamicVector(); // in repository.cpp it's not working. The error:undefined reference to `DynamicVector::DynamicVector()' – Cucerzan Rares Mar 31 '13 at 12:31
  • You have some other problems not shown by the code examples provided (like missing include directives). Can you update your question with your actual code? – Chad Mar 31 '13 at 12:33
  • @CucerzanRares: As per your latest error: could you post the class definition of `DynamicVector` as well (i.e. not only the constructor). Is the constructor declared public? – Tomas Aschan Mar 31 '13 at 12:51
  • @TomasLycken I updated the code. Yes, the constructor is public – Cucerzan Rares Mar 31 '13 at 12:57
  • @CucerzanRares: I tried to reproduce the error, but my code compiles fine: http://pastebin.com/Bd1byiND (I compile it by saving it in scratch.cpp and running `make scratch.o`) – Tomas Aschan Mar 31 '13 at 13:12
0

Constructor should be:

Repository::Repository(){
    this->MedList=new DynamicVector<Medicine>;
}

DynamicVector() calls the constructor for DynamicVector.

DynamicVector::DynamicVector() is a pointer to the address of the constructor function

maditya
  • 8,626
  • 2
  • 28
  • 28
  • it's not working either. Thanks! This is the error now: Multiple markers at this line - candidate is: - no match for 'operator=' in '((Repository*)this)->Repository::MedList = (operator new(12u), (, ((DynamicVector*))))' – Cucerzan Rares Mar 31 '13 at 12:16
  • No, error: undefined reference to `DynamicVector::DynamicVector()' – Cucerzan Rares Mar 31 '13 at 12:26
  • You shouldn't be getting an undefined reference unless you put the definition of the DynamicVector constructor in a separate file. If the definition of the constructor is in the same header file that contains the DynamicVector class template and you're still getting an undefined reference error, I'm sorry but don't know what's wrong ... – maditya Mar 31 '13 at 12:33
0

The chances are your C++ version doesn't allow empty () for constructors.

this->MedList=new DynamicVector<Medicine>::DynamicVector(); //error

should be

this->MedList=new DynamicVector<Medicine>::DynamicVector; 

or (The usual way of writing it)

this->MedList=new DynamicVector<Medicine>;

See here for more info.

EDIT. Make sure you have declared the dynamicVector constructor in the class.


Default constructor with empty brackets

Do the parentheses after the type name make a difference with new?

Community
  • 1
  • 1