#pragma once
class Population
{
int *numbers;
int rSize;
bool rSort;
public:
Population();
Population(int [], int n);
Population(const Population &);
Population & operator = (const Population &);
void load(string);
void add(string);
int getSize(int arr[], int arrSize);
int getSum(int arr[], int arrSize);
float getMean();
float getMedian();
float getStddev();
int *mode(int &);
~Population(void);
void printStatistics();
};
#include "Population.h"
#include <iostream>
#include <string>
#include <fstream>
#include <stdexcept>
usingnamespace std;
Population::Population(int [], int n)
{
numbers = newint[n]; // create a dynamic array with numbers to load the text file into
}
void Population::load(string a1test1)
{
int arrSize;
ifstream ist(a1test1.c_str()); // open file
// Check if file opened correctly
if(ist.fail()) throw runtime_error("file not found");
// Read first value into size attribute
ist >> arrSize;
numbers = newint[arrSize];
for (int i = 0; i < arrSize; i++) {
ist >> numbers[i];
}
// Create and assign new dynamic array to arr
// Read remaining values into array by iterating
// through the file contents reading each line:
// ist >> arr[i]
ist.close();
}
int Population::getSum(int arr[], int arrSize) {
int sum = 0;
for (int i = 0; i < arrSize; i++) {
sum = sum + arr[i];
}
return sum;
}
Population::Population(const Population & pop)
{
Popo(pop);
}
void Population::printStatistics() {
int size=0;
int* numbers;
numbers = newint[size];
cout << "Sum: " << getSum(numbers, size) << endl;
}
Population::~Population(void)
{
delete [] numbers;
}
Since you've wisely not put usingnamespace std; in the header file (and that's a good thing - you shouldn't put "using namespace" statements in headers), you'll need to explicitly put the namespace qualifier, i.e. std::string
Look at what the error message is telling you. It's telling you that you're trying to call Population::Population() (i.e. the default constructor), and it can't find a definition for that.
And if you look at your code, lo and behold - on line 12 of main.cpp, you're attempting to create a Population object with no arguments, which means you're trying to call the default constructor, and in your cpp file, there's no default constructor defined.
Yea, read the post and realized my reply was useless, and you probably weren't allowed to use 'em. I tried to delete it in time but then you replied. :P
I see numerous problems with the code, although the last good look appears in the opening post. Have you changed it much since then?
The biggest problem I see is with your use of dynamic allocation for the array to store the data from your text file.
I see arrays allocated to an int* named numbers all over the place. There's one in the main(), your load(), your printStatistics() and in the constructor.
If the numbers array is to be a member of the Population class then new should appear only in the constructors, operator= and the destructor, and delete should appear only in the destructor (which it does).
How many ways are there for constructing a Population object? If the only intended way is from data in a text file then there should only be one constructor, which itself reads the data from the file (so it could take a std::string as its sole argument). This constructor would be written like your existing load() and would replace it.
As I see your task, I think your class declaration should be more like this:
class Population
{
int *numbers;
int rSize;
// bool rSort;
public:
Population(std::string fileName);
// Population(int [], int n);
Population(const Population &);
Population & operator = (const Population &);
// void load(string);
// void add(string);// purpose?
// int getSize(int arr[], int arrSize);
int getSize();
// int getSum(int arr[], int arrSize);
int getSum();
float getMean();
float getMedian();
float getStddev();
// int *mode(int &);// purpose?
~Population(void);
void printStatistics();
};
I'm a beginner so bear with me please. The reason I have allocated int to numbers in main(), load() because it kept giving me errors that its undefined.
All I'm trying to do at the moment is to load the text file, store it in numbers and use it in getSum. Then make a function called printStatistics and call getSum in there.
Once that's done, I will call printStatistics in main(), however it's not working that well.
As for the header file, there are specific instructions that I have to follow for this assignment so I haven't considered std::string and such.
class Population
{
int *numbers;
int rSize;
// bool rSort;
public:
Population(std::string fileName); <- can't do this.. yet
// Population(int [], int n);
Population(const Population &);
Population & operator = (const Population &);
// void load(string);
// void add(string);// purpose? <- was in the instructions
// int getSize(int arr[], int arrSize);
int getSize();
// int getSum(int arr[], int arrSize);
int getSum();
float getMean();
float getMedian();
float getStddev();
// int *mode(int &);// purpose? <-- to calculate the mode (haven't written // that yet)
~Population(void);
void printStatistics();
};
#pragma once
#include <string>
usingnamespace std;
class Population
{
int *numbers;
int rSize;
bool rSort;
//void Popo(const Population & pop);
public:
Population(int [], int n);
Population(const Population & pop);
Population & operator = (const Population &); // overloaded operators
void load();
void add();
int getSize(int arr[], int arrSize);
int getSum(int arr[], int arrSize);
float getMean();
float getMedian();
float getStddev();
int *mode(int &);
~Population(void);
};
#include "Population.h"
#include <iostream>
#include <string>
#include <fstream>
#include <stdexcept>
usingnamespace std;
Population::Population(int [], int n)
{
numbers = newint[n]; // create a dynamic array with numbers to load the text file into
}
void Population::load()
{
int arrSize;
int *numbers;
ifstream ist;
ist.open("a1test1.txt", ios::in); // open file
// Check if file opened correctly
if(ist.fail()) throw runtime_error("file not found");
// Read first value into size attribute
ist >> arrSize;
numbers = newint[arrSize];
for (int i = 0; i < arrSize; i++) {
ist >> numbers[i];
}
// Create and assign new dynamic array to arr
// Read remaining values into array by iterating
// through the file contents reading each line:
// ist >> arr[i]
ist.close();
}
int Population::getSum(int numbers[], int arrSize) {
int sum = 0;
for (int i = 0; i < arrSize; i++) {
sum = sum + numbers[i];
}
return sum;
}
/*
Population::Population(const Population & pop)
{
Popo(pop);
}
*/
Population::~Population(void)
{
delete [] numbers;
}
I see this in your code #include <string> .
What type of string is that in void load(string); and void add(string); ?
You're not supposed to have a constructor Population(string);?
We were given the member functions and two of them were the load(string) and add(string).
I kept getting errors regarding identifier 'string' so I added the include and and namespace (even though I shouldn't have in the header) and the error went away.
As for Population(string), it wasn't given to us so not using it...