Reputation: 61
I am trying to create a dynamic array using a class. In my header file I have the following code:
#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
And in my .cpp file I have the following code:
#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()
{
}
I am getting a Out-of-line declaration of a member must be a definition compile error on both of the lines:
float Array::getTings (int index);
and
void Array::addTings (float itemValue);
Does anyone have any idea why? I thought I had linked the header file to the cpp file correctly but obviously not?
Upvotes: 3
Views: 14594
Reputation: 496
Get rid of the semi-colons in the definition of the functions. See below
void Array::addTings (float itemValue);
{
}
float Array::getTings (int index);
{
}
void Array::addTings (float itemValue)
{
}
float Array::getTings (int index)
{
}
Upvotes: 1
Reputation: 1863
You should remove the semicolons in the cpp file.
void Array::addTings (float itemValue);
should be
void Array::addTings (float itemValue)
Correct code is:
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
{
}
Upvotes: 12