473,407 Members | 2,315 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

inheritance first go

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.
Nov 9 '06 #1
3 1975
Gary Wessle wrote:
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().
Nov 9 '06 #2
Rolf Magnus <ra******@t-online.dewrites:
Gary Wessle wrote:
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.
....

}

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().
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
************************************************** **************
Nov 9 '06 #3

"Gary Wessle" <ph****@yahoo.comwrote in message
news:m3************@localhost.localdomain...
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

Nov 10 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Robert Spoons | last post by:
Can you look over this code, preferably try it, and comment? I believe the 'extend' function below will allow you to use full 'class inheritance' in javascript, but I would like to verify it. ...
20
by: Steve Jorgensen | last post by:
A while back, I started boning up on Software Engineering best practices and learning about Agile programming. In the process, I've become much more committed to removing duplication in code at a...
14
by: Steve Jorgensen | last post by:
Recently, I tried and did a poor job explaining an idea I've had for handling a particular case of implementation inheritance that would be easy and obvious in a fully OOP language, but is not at...
12
by: Steve Jorgensen | last post by:
The classing Visual Basic and VBA support for polymorphism, let's face it, is a bit on the weak side, and built-in support for inheritance is non-existent. This little essay is about some patterns...
22
by: Matthew Louden | last post by:
I want to know why C# doesnt support multiple inheritance? But why we can inherit multiple interfaces instead? I know this is the rule, but I dont understand why. Can anyone give me some concrete...
45
by: Ben Blank | last post by:
I'm writing a family of classes which all inherit most of their methods and code (including constructors) from a single base class. When attempting to instance one of the derived classes using...
6
by: VR | last post by:
Hi, I read about Master Pages in ASP.Net 2.0 and after implementing some WinForms Visual Inheritance I tryed it with WebForms (let's say .aspx pages, my MasterPage does not have a form tag itself...
15
by: scorpion53061 | last post by:
I am just now really starting to get into inheritance - controls, datasets, views all that stuff and I am seeing the enormous savings in performance and even watching the exe file size go down...
5
by: a | last post by:
Hi, I have an oop inheritance graph problem. What is the difference betweent the following 2 inheritance graph? How does the C++ solve the naming conflict problem for multiple inheritance...
3
by: Leo Seccia | last post by:
Hello everyone, I have a c# project with a sql server database. I have a number of lookup tables in my database which I successfully managed to import into my LINQ dataclasses. eg. Table:...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.