所以我有一个类y被使用的类x,它也将被其他类使用。错误未定义的引用.. C++?
.H类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对于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;
}
这是类.Hÿ
#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;
};
并且这对于类在.cpp Ÿ
#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();
}
}
}
种
方法在y类使用指针方法Y的数组,我想使用数组成员使用从类X的一些方法,但我得到一个错误说:
undefined reference to `x::x(...)
我认为它有与预处理器或标题有关。
缺少类“X”整个实现其定义(x.cpp)? – loentar
对不起,我只是没有粘贴。我现在将 – user3348712