【问题标题】:C++, Class: Out-of-line declaration of a member error?C++,类:成员错误的行外声明?
【发布时间】:2015-01-18 09:05:37
【问题描述】:

我正在尝试使用类创建动态数组。在我的头文件中,我有以下代码:

#ifndef DYNAMICARRAY
#define DYNAMICARRAY
#include <iostream>

class Array 
{

public:

    Array(); // Constructor - Initialises the data members
    ~Array(); // Destructor - That deletes the memory allocated to the array
    void addTings (float itemValue); // which adds new items to the end of the array
    float getTings (int index); // which returns the item at the index
    void size(); // which returns the number of items currently in the array

private:

    int arraySize;
    float  *floatPointer = nullptr;
};

#endif // DYNAMICARRAY

在我的 .cpp 文件中,我有以下代码:

#include "DYNAMICARRAY.h"

Array::Array()
{
    floatPointer = new float[arraySize];
}

Array::~Array()
{
    delete[] floatPointer;
}

void Array::addTings (float itemValue);  // Out-of-line declaration ERROR
{
    std::cout << "How many items do you want to add to the array";
    std::cin >> arraySize;
}

float Array::getTings (int index);    // Out-of-line declaration ERROR
{

}

void Array::size()
{

}

我得到一个成员的 Out-of-line 声明必须是两行上的定义编译错误:

float Array::getTings (int index);

void Array::addTings (float itemValue);

有人知道为什么吗?我以为我已将头文件正确链接到 cpp 文件但显然没有?

【问题讨论】:

  • 分号。分号太多...
  • 如果我复制你的数组对象会发生什么?

标签: c++ arrays class compiler-errors declaration


【解决方案1】:

您应该删除 cpp 文件中的分号。

void Array::addTings (float itemValue);

应该是

void Array::addTings (float itemValue)

正确的代码是:

void Array::addTings (float itemValue)  // Out-of-line declaration ERROR
{
std::cout << "How many items do you want to add to the array";
std::cin >> arraySize;


}

float Array::getTings (int index)    // Out-of-line declaration ERROR
{

}

【讨论】:

    【解决方案2】:

    去掉函数定义中的分号。见下文

    void Array::addTings (float itemValue);
    {
    }
    float Array::getTings (int index);
    {
    }
    
    void Array::addTings (float itemValue)
    {
    }
    float Array::getTings (int index)
    {
    }
    

    【讨论】:

      猜你喜欢
      • 2012-06-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多