LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-16-2011, 02:31 PM   #16
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197

Suddenly I see the problem. It is in the std::string, not in the AbstractObject.

Quote:
Originally Posted by MTK358 View Post
Here is the signature:

Code:
virtual void set(std::string &name, AbstractObject *value);
Here is the call that causes the error:

Code:
set(std::string("print"), new Func_print(this));
std::string("print") creates a temporary object. In C++ you can pass a temporary object as a parameter to a function by value or by const&. You cannot pass it by non const &.

If you don't want to change the declaration of set(), you need to make the temporary object a local object instead, so the correct call would look like:

Code:
{
std::string local_string("print");
set(local_string, new Func_print(this));
}
Notice the {} around the definition and use of local_string. Those make it just as temporary as the unnamed string in your version. But according to the language rules it is a local object, not a temporary object, so passing it by non const & is OK.

However, if I looked at your whole program I'd probably suggest changing the declaration of set() instead. Unless I'm totally wrong in guessing the nature of that function, passing it a std::string& is bad design. It should be std::string const&

Last edited by johnsfine; 01-16-2011 at 02:46 PM.
 
Old 01-16-2011, 02:34 PM   #17
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443

Original Poster
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Quote:
Originally Posted by johnsfine View Post
Is AbstractFunction a public base class of Func_print?
Is AbstractObject a public base class of AbstractFunction?
What does that mean?

Quote:
Originally Posted by johnsfine View Post
What declarations of set() are in IntegerPrimitive? Any chance such declaration hides the base class declaration of set() that you want?
None.

Quote:
Originally Posted by johnsfine View Post
Missing that would give a different error message (if it were an error at all).
I tried making it virtual to see if it would get rid of the error.
 
Old 01-16-2011, 02:52 PM   #18
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by MTK358 View Post
What does that mean?
I thought it was a reasonable and relevant request for clarification of what you said in post #1. I'm surprised you don't understand what I was asking. But anyway don't worry about it, because I only asked that as a result of missing your post #13 (and thus fishing for unlikely complications).

In case you missed it while we were cross posting, read my post
http://www.linuxquestions.org/questi...6/#post4226427
for explanation of your coding error.
 
Old 01-16-2011, 03:04 PM   #19
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi, MTK358 -

No offense - but you really, REALLY need to step back and review a few more C++ basics before you continue on this course. A few book recommendations:

* Programming Principles and Practice Using C++: Bjarne Stroustrup

* The C++ Programming Language: Bjarne Stroustrup

* Effective C++: Scott Meyers


You're running up against my single biggest objection to C++: there are far too many subtleties that have nothing to do with the problem you're trying to solve, but everything to do with the (over-complicated) language.

Please do yourself a BIG favor and do some more background reading first. If you understand the difference between a public base class and a private base class (and why you almost always use the former), or why if you have a virtual base class, you should almost always have a virtual destructor - then you know you're probably heading in the right direction

Or you can just learn Java, Python or Free Pascal instead

IMHO .. PSM
 
2 members found this post helpful.
Old 01-16-2011, 03:38 PM   #20
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443

Original Poster
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Quote:
Originally Posted by paulsm4 View Post
You're running up against my single biggest objection to C++: there are far too many subtleties that have nothing to do with the problem you're trying to solve, but everything to do with the (over-complicated) language.
I also dislike C++ for the same reason and would like to use something else, but I don't see any other good choices.

My project is a simple language interpreter, now trying to implement scopes, functions, and objects (by storing scopes in varibles) rather than being limited to integers.
 
Old 01-16-2011, 03:43 PM   #21
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by paulsm4 View Post
Hi, MTK358 -

No offense - but you really, REALLY need to step back and review a few more C++ basics before you continue on this course. A few book recommendations:

* Programming Principles and Practice Using C++: Bjarne Stroustrup

* The C++ Programming Language: Bjarne Stroustrup

* Effective C++: Scott Meyers


You're running up against my single biggest objection to C++: there are far too many subtleties that have nothing to do with the problem you're trying to solve, but everything to do with the (over-complicated) language.

Please do yourself a BIG favor and do some more background reading first. If you understand the difference between a public base class and a private base class (and why you almost always use the former), or why if you have a virtual base class, you should almost always have a virtual destructor - then you know you're probably heading in the right direction

Or you can just learn Java, Python or Free Pascal instead

IMHO .. PSM
And, of course http://openassist.googlecode.com/fil...882%202003.pdf (the standard) - reading this I see quite a few really nasty gotchas, like, for example, initialization of structures with some static members - arrrgh !!!

I.e. I agree with paulsm4 regarding "(over-complicated) language".
 
Old 01-16-2011, 06:29 PM   #22
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by paulsm4 View Post
you really, REALLY need to step back and review a few more C++ basics before you continue on this course. A few book recommendations:
I think learning by doing ends up working better. You make mistakes, such as the one I explained in post #16 of this thread, then you find out what is wrong, then hopefully you understand the issue next time.

Learning by reading books doesn't work as well.

Quote:
You're running up against my single biggest objection to C++: there are far too many subtleties that have nothing to do with the problem you're trying to solve, but everything to do with the (over-complicated) language.
I absolutely agree. That is what makes C++ a terrible language. Within that, the rules for matching the parameter types in a function call to the types in a function declaration (which tripped up the OP in this thread) may be the nastiest most over-complicated aspect of the language.

But despite being a terrible language, C++ is far better than any other popular programming language.

Quote:
If you understand the difference between a public base class and a private base class (and why you almost always use the former)
I almost always use public base classes. Many experts disagree and almost always use private base classes.

Quote:
or why if you have a virtual base class
Most people would take that phrase to mean virtual inheritance of a base class. But I think you might mean simply having a base class that has virtual functions (very different from virtual inheritance).

Quote:
, you should almost always have a virtual destructor
For either meaning of the earlier phrase that is not a particularly helpful rule.

Quote:
- then you know you're probably heading in the right direction
It's hard to even imagine the point you're trying to make with that.

Quote:
Or you can just learn Java, Python or Free Pascal instead

IMHO .. PSM
IM~HO, none of those do the job C++ does.

Last edited by johnsfine; 01-16-2011 at 06:32 PM.
 
Old 01-17-2011, 07:18 AM   #23
sunnydrake
Member
 
Registered: Jul 2009
Location: Kiev,Ukraine
Distribution: Ubuntu,Slax,RedHat
Posts: 289
Blog Entries: 1

Rep: Reputation: 61
check in/out params maybe set just expect different type.number of variables to call.
btw: gcc is pia if virtual is not defined in place virtual void mustbeset(){}; can be overloaded later.

Last edited by sunnydrake; 01-17-2011 at 07:33 AM.
 
Old 01-17-2011, 09:47 AM   #24
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443

Original Poster
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Somehow I tried again today and it doesn't have that error. Now it says:

Code:
$ make
Linking CXX executable lang
CMakeFiles/lang.dir/IntegerPrimitive.cpp.o: In function `IntegerPrimitive::IntegerPrimitive(std::basic_string<char, std::char_traits<char>, std::allocator<char> >&)':
IntegerPrimitive.cpp:(.text+0x104): undefined reference to `Scope::~Scope()'
collect2: ld returned 1 exit status
make[2]: *** [src/lang] Error 1
make[1]: *** [src/CMakeFiles/lang.dir/all] Error 2
make: *** [all] Error 2
 
Old 01-17-2011, 09:50 AM   #25
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
Where is the definition of that destructor?
I think you should read up this: http://www.crasseux.com/books/ctutor...html#Debugging

and it is also better to read the error message and try to Google first.

Last edited by Aquarius_Girl; 01-17-2011 at 09:53 AM.
 
1 members found this post helpful.
Old 01-17-2011, 10:04 AM   #26
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443

Original Poster
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Quote:
Originally Posted by Anisha Kaul View Post
Where is the definition of that destructor?
Nowhere.
 
Old 01-17-2011, 10:08 AM   #27
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
Quote:
Originally Posted by MTK358 View Post
Nowhere.
Very nice, did you click the link I posted?
 
Old 01-17-2011, 10:18 AM   #28
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443

Original Poster
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
I fixed it now.
 
Old 01-17-2011, 10:23 AM   #29
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by MTK358 View Post
undefined reference to `Scope::~Scope()'
That error at link time normally means that function has been declared but not defined.

If an ordinary function were used not declared, you would get an error at compile time not link time. So for an ordinary function that error would imply used and declared but not defined.

Failing to declare a destructor should not give an error at either compile or link time. The compiler provides a destructor if you don't declare one.

By declaring the destructor, you keep the compiler from providing it. Then by not defining it, you cause this error. (So details are different between ordinary and special functions but the bottom line of declared but not defined is the same).

Quote:
Originally Posted by MTK358 View Post
I fixed it now.
Hopefully you won't need to ask such questions in the future. You posted a pretty clear error message implying the function wasn't defined. Before asking for any help, you should have looked at the definition of that function (in this case discovering you didn't have one).

There are obscure causes for almost any clear looking error message. So after you look into the obvious meaning of the error message, you might occasionally still need help. But don't make asking for help your first step in diagnosing a problem.

BTW, you might notice the error was reported for the constructor. A non virtual function declared but not defined is only an error if the function is also used. But your destructor is virtual. An virtual function declared but undefined causes an error at link time in the constructor (assuming the constructor is either defined or used).

Last edited by johnsfine; 01-17-2011 at 10:34 AM.
 
2 members found this post helpful.
Old 01-18-2011, 11:11 AM   #30
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443

Original Poster
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Just one more thing:

Code:
class AbstractFunction : public AbstractObject
{
public:
    virtual AbstractObject *call(std::vector<AbstractObject*> const &params) = 0; // I also tried "{}", "{ return NULL; }", and ";"
};
And when I call it:

Code:
((AbstractFunction*) (s->get("print")))->call(std::vector<AbstractObject*>());
It says:

Code:
Scanning dependencies of target lang
[ 14%] Building CXX object src/CMakeFiles/lang.dir/IntegerPrimitive.cpp.o
Linking CXX executable lang
CMakeFiles/lang.dir/main.cpp.o: In function `main':
main.cpp:(.text+0xc0): undefined reference to `AbstractFunction::call(std::vector<AbstractObject*, std::allocator<AbstractObject*> >)'
collect2: ld returned 1 exit status
make[2]: *** [src/lang] Error 1
make[1]: *** [src/CMakeFiles/lang.dir/all] Error 2
make: *** [all] Error 2
But AbstractFunction is an abstract class, and I don't want call() defined.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
repeatable disk read/write errors with no errors logged by kernel or SMART mxl2 Linux - Hardware 9 04-02-2011 09:27 AM
yum update transaction check errors & mirror errors tonycunn Linux - Software 2 11-29-2010 08:16 AM
Errors, Errors, and more Errors (KDE 3.4.x GUI Errors) Dralnu Linux - Software 2 05-13-2006 08:30 AM
Errors during filesystem check with one kernel while no errors with other kernel Potentials Linux - General 11 12-30-2003 04:24 AM
QMAIL errors errors.. YourForum Linux - Software 0 11-27-2003 12:30 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:28 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration