inheritance first go

G

Gary Wessle

Hi
this code of mine, is my first attempt at inheritance. it is buggy and
I am sure there are few errors, it did not compile and I could not
find out why, please help.

thank you

#include <string>
#include <sstream>
#include <fstream>
#include <iostream>
using namespace std;


class Task {

protected:
string f_path;
short s_status;
string name;
string menu_item;

void load_status() {
string sf = f_path + name + "_s.txt";
ifstream in( sf.c_str() );
string s;
getline(in, s);
stringstream st(s.c_str());
st >> s_status;
}

public:
Task(string n, string mi) :
name( n ),
menu_item( mi ),
f_path( "../str1/status/" )
{
load_status();
}

void p_menu_item() const {
cout << menu_item << endl;
}
virtual void preform();

};


class Thr_task : public Task {
public:
Thr_task(string n, string mi) :
Task(n, mi) {}

void preform();
void pref_thr();
};

class No_thr_task : public Task {
public:
No_thr_task(string n, string mi):
Task(n, mi){}

void preform();
};





int main(){
No_thr_task nt1("sometask", "Taaask str1");
Thr_task t1("optm_upd", "Update and optimization");

nt1.p_menu_item;
t1.p_menu_item;

}

the errors relate to function overloading for the 2 lines above, and
also in relation to vtable and references.
 
R

Rolf Magnus

Gary said:
Hi
this code of mine, is my first attempt at inheritance. it is buggy and
I am sure there are few errors, it did not compile and I could not
find out why, please help.

thank you

#include <string>
#include <sstream>
#include <fstream>
#include <iostream>
using namespace std;


class Task {

protected:
string f_path;
short s_status;
string name;
string menu_item;

void load_status() {
string sf = f_path + name + "_s.txt";
ifstream in( sf.c_str() );
string s;
getline(in, s);
stringstream st(s.c_str());

No need for using c_str() here.
st >> s_status;
}

public:
Task(string n, string mi) :
name( n ),
menu_item( mi ),
f_path( "../str1/status/" )
{
load_status();
}

void p_menu_item() const {
cout << menu_item << endl;
}
virtual void preform();

};


class Thr_task : public Task {
public:
Thr_task(string n, string mi) :
Task(n, mi) {}

void preform();
void pref_thr();
};

class No_thr_task : public Task {
public:
No_thr_task(string n, string mi):
Task(n, mi){}

void preform();
};





int main(){
No_thr_task nt1("sometask", "Taaask str1");
Thr_task t1("optm_upd", "Update and optimization");

nt1.p_menu_item;
t1.p_menu_item;

}

the errors

Which errors? Please always paste the original error message from your
compiler into the posting. You might not know what their meaning is, but
many people here do, so it make it easier to help you. Best is to also
directly mark the lines the error messages are referring to with a comment.
relate to function overloading for the 2 lines above, and
also in relation to vtable and references.

Well, you have three classes, and in each of them, a function preform() is
declared, but there is no implementation of any of those three functions.
Same for Thr_task::pref_thr().
 
G

Gary Wessle

Rolf Magnus said:
Which errors? Please always paste the original error message from your
compiler into the posting. You might not know what their meaning is, but
many people here do, so it make it easier to help you. Best is to also
directly mark the lines the error messages are referring to with a comment.


Well, you have three classes, and in each of them, a function preform() is
declared, but there is no implementation of any of those three functions.
Same for Thr_task::pref_thr().

well, I thought the virtual key word did just that, I don't want to
make it pure virtual (=0) for fear that I may need to create an
instance of the base, well, maybe I should try the pure virtual.

cd /home/fred/myProg/try/
make -k
g++ -gdwarf-2 -c -o main.o main.cpp
main.cpp: In function ‘int main()’:
main.cpp:80: error: statement cannot resolve address of overloaded function
main.cpp:81: error: statement cannot resolve address of overloaded function
make: *** [main.o] Error 1
make: Target `proj' not remade because of errors.

Compilation exited abnormally with code 2 at Fri Nov 10 05:16:48

the lines 80, 81 are
nt1.p_menu_item;
t1.p_menu_item;


and when I comment them I get:

cd /home/fred/myProg/try/
make -k
g++ -gdwarf-2 -c -o main.o main.cpp
g++ -Wall -gdwarf-2 -o proj handle.o main.o swi_board.o
main.o: In function `~Task':
/home/fred/myProg/try/main.cpp:17: undefined reference to `vtable for Task'
main.o: In function `~Thr_task':
/home/fred/myProg/try/main.cpp:52: undefined reference to `vtable for Thr_task'
main.o: In function `~No_thr_task':
/home/fred/myProg/try/main.cpp:61: undefined reference to `vtable for No_thr_task'
main.o: In function `Task':
/home/fred/myProg/try/main.cpp:39: undefined reference to `vtable for Task'
main.o: In function `No_thr_task':
/home/fred/myProg/try/main.cpp:64: undefined reference to `vtable for No_thr_task'
main.o: In function `Thr_task':
/home/fred/myProg/try/main.cpp:55: undefined reference to `vtable for Thr_task'
collect2: ld returned 1 exit status
make: *** [proj] Error 1

Compilation exited abnormally with code 2 at Fri Nov 10 05:18:27

here is the code again but with line numbers this time.

****************************************************************
1 #include <string>
2 using std::string;
3 #include <sstream>
4 using std::stringstream;
5 #include <fstream>
6 using std::ifstream;
7 #include <iostream>
8 using std::cout;
9 using std::endl;
10
11 // #include "swi_board.h"
12 // #include "handle.h"
13
14 using namespace std;
15
16
17 class Task {
18
19
20 protected:
21 string f_path;
22 short s_status;
23 string name;
24 string menu_item;
25
26 void load_status() {
27 string sf = f_path + name + "_s.txt";
28 ifstream in( sf.c_str() );
29 string s;
30 getline(in, s);
31 stringstream st(s);
32 st >> s_status;
33 }
34
35 public:
36 Task(string n, string mi) :
37 name( n ),
38 menu_item( mi ),
39 f_path( "../str1/status/" )
40 {
41 load_status();
42 }
43
44 void p_menu_item() const {
45 cout << menu_item << endl;
46 }
47 virtual void preform();
48
49 };
50
51
52 class Thr_task : public Task {
53 public:
54 Thr_task(string n, string mi) :
55 Task(n, mi) {}
56
57 void preform();
58 void pref_thr();
59 };
60
61 class No_thr_task : public Task {
62 public:
63 No_thr_task(string n, string mi):
64 Task(n, mi){}
65
66 void preform();
67 };
68
69
70
71
72
73 int main(){
74 // Handle ha; // control pointer to functions
75 // Switch sw(ha); // load the switches
76 // cout << "handle" << endl;
77 No_thr_task nt1("sometask", "Taaask str1");
78 Thr_task t1("optm_upd", "Update and optimization");
79
80 // nt1.p_menu_item;
81 // t1.p_menu_item;
82
83
84
85 }
86
****************************************************************
 
H

Howard

72
73 int main(){
74 // Handle ha; // control pointer to functions
75 // Switch sw(ha); // load the switches
76 // cout << "handle" << endl;
77 No_thr_task nt1("sometask", "Taaask str1");
78 Thr_task t1("optm_upd", "Update and optimization");
79
80 // nt1.p_menu_item;
81 // t1.p_menu_item;

Where are the parentheses? You call those functions like this:

nt.p_menu_item();
t1.p_menu_item();

(By the way, the suggestion to add comments meant that you should add a
comment somewhere immedately before or after the line in error, saying
something lke "This is the error line!" or "This is line 80", not that you
should comment out the offending line.)

-Howard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,996
Messages
2,570,237
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top