C++,类 : Out-of-line declaration of a member error?

标签 c++ arrays class compiler-errors declaration

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

#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()
{

}

我得到一个成员的外联声明必须是两行的定义编译错误:

float Array::getTings (int index);

void Array::addTings (float itemValue);

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

最佳答案

您应该删除 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
{

}

关于C++,类 : Out-of-line declaration of a member error?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27040692/

相关文章:

c++ - 使用 libpng 将转换设置为灰度

C++ - 成员初始化和递增的顺序

c++ - C 字符串 :\n & ' ' not detected

c++ - 将指针的地址传递给需要指向指针的函数有什么意义?

java - 分解任何字符串

java - "Code too large"Java编译错误

javascript - 如何处理 json

c++ - 如何使用在构造函数中创建的对象

c++ - 为引用成员变量提供默认值

c++ - 带有 Visual Assist X 和 Doxygen 的 Visual Studio 2012 IntelliSense