linker error
I'm getting a linker error in my code and I don't know why?
Header
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
#undef WATCH_H
#ifndef WATCH_H
#define WATCH_H
#include <time.h>
class watch
{
public:
watch();
void start();
void stop();
void delay(int duration);
float seconds();
float minutes();
float hours();
private:
clock_t ticks_passed;
};
#endif /* WATCH_H_hpp */
|
driver file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
|
#include <iostream>
#include <stdlib.h>
#include "WATCH_H.hpp"
using namespace std;
watch :: watch()
{
ticks_passed = clock();
}
void watch:: start()
{
ticks_passed = clock();
}
void watch:: stop()
{
ticks_passed = clock() - ticks_passed;
if(ticks_passed == clock_t(-1))
{
cout << "Clock overflow" << endl;
exit(1);
}
}
void watch:: delay(int duration)
{
for(int i =0; i<duration; ++i);
}
float watch:: seconds()
{
return float(ticks_passed)/CLOCKS_PER_SEC;
}
float watch :: minutes()
{
return float(ticks_passed)/CLOCKS_PER_SEC/60;
}
float watch :: hours()
{
return float(ticks_passed)/CLOCKS_PER_SEC/60/60;
}
|
Implementation
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
|
#include <iostream>
#include <ctime>
#include <vector>
#include <iomanip>
#include <algorithm>
#include "WATCH_H.hpp"
const int SIZE =10000;
unsigned int seed = (unsigned)time(0);
using namespace std;
void bubble_sort(vector <int>v);
double random (unsigned int &seed);
void initialize_vector(vector<int> &v);
void print_vector (vector<int> :: iterator b,
vector<int> :: iterator e );
int main()
{
vector <int> v;
watch my_watch;
initialize_vector(v);
my_watch.start();
bubble_sort(v);
my_watch.stop();
print_vector(v.begin(), v.end());
cout << "Seconds passed " << my_watch.seconds()<< endl;
my_watch.start();
sort(v.begin(),v.end());
my_watch.stop();
print_vector(v.begin(), v.end());
cout << "Seconds passed " << my_watch.seconds()<< endl;
return 0;
}
void initialize_vector(vector<int> &v)
{
for (int i=0; i<SIZE; i++)
v.push_back(random(seed)*11);
}
void bubble_sort(vector<int> &v)
{
int temp;
for(int k=1; k<v.size()-1; ++k)
for(int j=0;j<v.size()-1-k;++j)
if(v[j]>v[j+1])
{
temp= v[j];
v[j]=v[j+1];
v[j+1] = temp;
}
}
double random(unsigned int &seed)
{
const int MODULUS = 15749;
const int MULTIPLIER = 69069;
const int INCREMENT = 1;
seed = ((MULTIPLIER*seed)+INCREMENT)%MODULUS;
return double (seed)/MODULUS;
}
void print_vector (vector<int> :: iterator b,
vector<int> :: iterator e )
{
vector<int> :: iterator p =b;
while(p<e)
cout << setw(3) << (*p++);
cout << endl;
}
|
You prototype the bubble sort function as
void bubble_sort(vector<int>& v)
but you implement it as
void bubble_sort(vector<int> v)
Last edited on
Topic archived. No new replies allowed.