C,Class:成员错误的外线声明?
发布时间:2020-12-16 10:10:50 所属栏目:百科 来源:网络整理
导读:我正在尝试使用类创建动态数组.在我的头文件中,我有以下代码: #ifndef DYNAMICARRAY#define DYNAMICARRAY#include iostreamclass Array {public: Array(); // Constructor - Initialises the data members ~Array(); // Destructor - That deletes the memo
我正在尝试使用类创建动态数组.在我的头文件中,我有以下代码:
#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 { } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |