The following is a class I built. Compiling the cpp file I get NO errors. But when I build the .h file and .cpp file, I always get these 2 linker errors and I'm not sure why. I followed the step by step process of creating a project workspace using the Microsoft visual C++ compiler and made sure both files were inserted into that workspace. So if anyone could tell me why I'm getting linker errors, I'd really appreciate it. Thanks so much.
Code:
#define MAX_SIZE 20
class List
{
public:
List(); //starts list
//member functions
int Insert(int);//inserts a number at the end(if there is room)
int Insert(int, int);//if room and if valid position given, inserts value
//in middle
int Delete(int);//if given a position, deletes a value from list
int Size();//returns list size
int Sum(); //returns values' sum in list
float Average();//returns values' in list
int Evens();// returns how many even numbers in list after counting up
int Greater(int);//returns how many numbers are greater than a //given parameter value after counting up
int WhereIs(int);//gives position of first occurence of a value, if not //found then 0 is returned
int Smallest();//returns list's smallest value
void Clear();//resets list object to represent an empty list
void Show();//shows the contents of the list
private:
void CalculateSum(void);
int arr[MAX_SIZE];//size can be at most 20 integers
int sum;
int size;
};
Code:
//This file contains the declarations of the list header//
#include <iostream>
#include "list.h"
using namespace std;
//List member functions
List::List()
//gives user an empty list,
{
Clear();//clears list for inserting
}//end of constructor
int List::Insert (int value)
{
if (size<MAX_SIZE) { //determines if size is less than 20
arr[size++]=value;
CalculateSum();
return 1;
}
cout<<"*** Invalid List Position Number\n";
return 0;
}
int List::Insert (int pos, int value)
{
if ((pos>0) && (pos < size) && (size<MAX_SIZE)) {
for (int i=size;i>pos-1;i--) //loop
arr[i]=arr[i-1];
arr[pos-1]=value;
size++;
CalculateSum();
return 1;
}
cout<<"*** Invalid List Position Number\n";
return 0;
}
int List::Delete (int pos)
{
if ((pos>0) && (pos < size)) { //determines if position is greater
//than 0 and less than size
for (int i=pos-1;i<size;i++) //loop
arr[i]=arr[i+1];
size--;
CalculateSum();
return 1;
}
cout<<"*** Invalid List Position Number\n";
return 0;
}
int List::Size (void) { //returns size of list
return size;
}
float List::Average (void) { //returns average of list
return sum/size;
}
int List::Evens (void) {
int e=0;
for (int i=0;i<size;i++) //loop
if ((arr[i] % 2) == 0) //test for even
e++;//increment evens
return e;
}
int List::Greater(int x) {
int g=0;
for (int i=0;i<size;i++) //loop
if (arr[i]>x) //test for greater than
g++;//increment greater
return g;
}
int List::Smallest (void) {
int s=32767;
for (int i=0;i<size;i++) //loop
if (arr[i]<s) //test if arr[i] is smaller than s
s=arr[i]; //set s to arr[i]
return s;
}
int List::WhereIs(int x) {
for (int i=0;i<size;i++) //loop
if (arr[i]==x) //test if arr[i] is equal to x
return i+1;
return 0;
}
void List::CalculateSum(void) {
//zero out sum
sum=0;
//add all the numbers together
for (int i=0;i<size;i++)
sum+=arr[i];
}
int List::Sum(void) { //returning sum of list
return sum;
}
void List::Clear(void) {
//clears the array element by element
//set all to zero
for (int i=0;i<MAX_SIZE;i++)
arr[i]=0;
//reset the size
size=0;
}
void List::Show(void) { //displaying contents of list
cout << "Contents: ";
for (int i=0;i<size;i++)
cout << arr[i] << ", ";
cout << "\n";
}
Thanks a lot guys.