-1

so I have a class x that is being used by class y and it's also going to be used by other classes.

.h for class x

#pragma once
#include <string>
#ifndef X_H
#define X_H
class x
{
public:
    x();
    const std::string & getName() const;
    int getQuantity();

private:
    std::string name;
    int quantity;
};
#endif

.cpp for x

#include <string>
#include "x.h"
using namespace std;



x::x()
: name(),quantity(0)
{
}
const string & x::getName() const
{
return  name;
}
const string & x::getQuantity() const
{
return quantity;
}

this is the .h for class y

#pragma once
#include <string>
#include <array>
#include "x.h"

class y
{
public:
    static const size_t number = 20;

    y();
    float getTotal();

private:
    std::array<X*, number> arrayList;
};

and this is the .cpp for class y

#include "y.h"
#include "x.h"
#include <array>
#include <string>
#include <iostream>

using namespace std;

y::y()
: arrayList()
{
}

float y::getTotal()
{
    float total=0.0;
    for(int i=0; i< number; i++)
    {
        if(arrayList[i] != nullptr)
        {
            total += arrayList[i]->getQuantity();
        }
    }
}

methods in the y class uses an array of pointers to method y and I'm trying to use some methods from class x using the array members but I get an error saying:

undefined reference to `x::x(...)

I think it has something to do with the preprocessors or the headers.

user3348712
  • 161
  • 10

2 Answers2

0

This means that you forgot to define the default constructor X::X() or some other constructor with parameters ( what does x::x(...) mean?) of class X. You only declared it in the class definition. Or the other reason is that the module with the constructor definition was not included in the project build.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0

In class x you have explicitly declared the default constructor x() but you have not defined it. If you want to use the default constructor, remove its definition or define it with x::x():name(std::string()),quantity(0){}

WileTheCoyot
  • 513
  • 1
  • 5
  • 20