473,799 Members | 3,148 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Why are g++ error messages so daunting?

I have started with C++ a few months ago. The language itself is nice,
but what I really don't like are the error messages that I get from
g++.

g++ error messages are often just not helpful.

Sometimes I just take the line number of the error and try to figure
out myself what might be wrong. Or I just remember this error message
and know already: if g++ says error X it really means error Y.

For example, take the following code:

#include <iostream>
class Interface{
public:
virtual std::string &getText() const = 0;
};
class Implementation : public Interface{
std::string text;
public:
Implementation: :Implementation (std::string text):text(text ){}
std::string &getText() const{
return text;
}
};

bash-2.05b$ g++ -c test.cpp
test.cpp: In member function `virtual std::string&
Implementation: :getText()
const':
test.cpp:13: could not convert `this->Implementation ::text' to
`std::string&'

The return type of getText() must not be std::string, but const
std::string. But why doesn't g++ just say that? It could say: "Could
not return reference to this->... from a const method"...

For beginners, it's also intimidating to get an error message that
fills several screens from the STL usage
std::vector<std ::string> test;
std::cout << test;
The whole "candidates are..." list with 26 entries is anyway
unreadable... There should be a better, more readable way to indicate
an error.

Markus
Jul 22 '05 #1
11 2348
Markus Dehmann wrote:
I have started with C++ a few months ago.
The language itself is nice, but what I really don't like
are the error messages that I get from g++.

g++ error messages are often just not helpful. Sometimes I just take the line number of the error
and try to figure out myself what might be wrong.
Or I just remember this error message and know already:
if g++ says error X it really means error Y.

For example, take the following code:

#include <iostream>
class Interface {
public:
virtual std::string& getText(void) const = 0;
}; class Implementation: public Interface { private: std::string text;
public:
Implementation( std::string text):text(text ) { } virtual std::string& getText(void) const {
return text;
}
};

bash-2.05b$ g++ -c test.cpp
test.cpp: In member function `virtual std::string&
Implementation: :getText()
const':
test.cpp:13: could not convert `this->Implementation ::text' \
to `std::string&'

The return type of getText() must not be std::string but
const std::string. But why doesn't g++ just say that? It could say:
"Could not return reference to this->... from a const method"...

For beginners, it's also intimidating to get an error message that
fills several screens from the STL usage
std::vector<std ::string> test;
std::cout << test;
The whole "candidates are..." list with 26 entries is anyway
unreadable...
There should be a better, more readable way to indicate an error.
cat test.cpp #include <iostream>
class Interface {
public:
virtual
std::string& getText(void) const = 0;
};

class Implementation: public Interface {
private:
std::string text;
public:
Implementation( std::string text): text(text) { }
virtual
std::string &getText(voi d) const {
return text;
}
};
g++ -Wall -ansi -pedantic -c test.cpp test.cpp:8: warning: `class Implementation' \
has virtual functions but non-virtual destructor
test.cpp: In member function \
`virtual std::string& Implementation: :getText() const':
test.cpp:15: error: invalid initialization of reference \
of type 'std::string&' from expression \
of type 'const std::string' g++ --version

g++ (GCC) 3.3.3 20040412 (Red Hat Linux 3.3.3-7)

Try the gnu.g++.help newsgroup.
Also, try comparing the GNU C++ diagnostic messages
with the diagnostic messages produced by other compilers.
I think that you will find that they compare favorably.
Don't expect too much from compiler diagnostics.
The compiler can't know what you *intended* to write.
It doesn't pick up "understand ing" from comments
or your choice to type and variable names.
Diagnostics are pretty much constrained to the rules
of the C++ computer programming language
which, unfortunately, don't always help you understand
what you did wrong.
Jul 22 '05 #2
I tried fiddling with the code. Here's what I've got:

class Interface
{
public:
virtual std::string& GetText() const = 0;
};

class Implementation : public Interface
{
private:

std::string text;

public:

Implementation( const std::string& in_text) : text(in_text) { ; }

virtual std::string& GetText() const
{
return text;
}
};

int main()
{
std::string k = "PooPoo";

Implementation p(k);
}

I'm at a loss to explain why it won't compile. G++ is giving me the
following:
t:/poo.cpp: In member function `virtual std::string& Implementation: :GetText
()
const':
t:/poo.cpp:22: could not convert `this->Implementation ::text' to
`std::string&'

Anyone?

-JKop
Jul 22 '05 #3

"JKop" <NU**@NULL.NULL > wrote in message
news:Ax******** *********@news. indigo.ie...
I tried fiddling with the code. Here's what I've got:

class Interface
{
public:
virtual std::string& GetText() const = 0;
};

class Implementation : public Interface
{
private:

std::string text;

public:

Implementation( const std::string& in_text) : text(in_text) { ; }

virtual std::string& GetText() const
{
return text;
}
};

int main()
{
std::string k = "PooPoo";

Implementation p(k);
}

I'm at a loss to explain why it won't compile. G++ is giving me the
following:
t:/poo.cpp: In member function `virtual std::string& Implementation: :GetText ()
const':
t:/poo.cpp:22: could not convert `this->Implementation ::text' to
`std::string&'

Anyone?

You are trying to make a constant member function return a non-constant
reference to a member. The compiler is telling you it cannot convert a
const string& to a string&

DrX
Jul 22 '05 #4

Thanks.
The following compiles:
#include <string>
#include <iostream>

class Interface
{
public:
virtual std::string& GetText() = 0;
};

class Implementation : public Interface
{
private:

std::string text;

public:

Implementation( const std::string& in_text) : text(in_text) { ; }

virtual std::string& GetText()
{
return text;
}
};

int main()
{
std::string k = "PooPoo";

Implementation p(k);
}

-JKop
Jul 22 '05 #5

"JKop" <NU**@NULL.NULL > wrote in message
news:75******** *********@news. indigo.ie...

Thanks.
The following compiles:
#include <string>
#include <iostream>

class Interface
{
public:
virtual std::string& GetText() = 0;
};

class Implementation : public Interface
{
private:

std::string text;

public:

Implementation( const std::string& in_text) : text(in_text) { ; }

virtual std::string& GetText()
{
return text;
}
};

int main()
{
std::string k = "PooPoo";

Implementation p(k);
}

-JKop


Instead of giving the caller write access to the string, I would suggest
"going the way" to fix it. Meaning, keep the member function constant, and
change the return type to const std::string&

Jul 22 '05 #6
Xenos posted:
Instead of giving the caller write access to the string, I would
suggest "going the way" to fix it. Meaning, keep the member function
constant, and change the return type to const std::string&

I was just trying to get the code to compile without interferring. Good
points though. But then some may say that you should use an accessor
function and return by value... which is a good point also.

-JKop
Jul 22 '05 #7
JKop wrote:
Xenos posted:

Instead of giving the caller write access to the string, I would
suggest "going the way" to fix it. Meaning, keep the member function
constant, and change the return type to const std::string&


I was just trying to get the code to compile without interferring. Good
points though. But then some may say that you should use an accessor
function and return by value... which is a good point also.

-JKop

returning by value implies a copy (which the compiler may then optimize
away) whereas returning a const reference doesn't (so there's less to do
for the optimizer). Hence

const std::string & MyClass::GetTex t(void) const;

is preferable to

std::string MyClass::GetTex t(void) const;

because it doesn't rely on the compiler to optimize the extra copy away.

Note, however, that it does rely on the type of text in the class not
changing: passing back a value is better if you think the type of your
text member variable may change from std::string to something else..

HTH

rlc
Jul 22 '05 #8
Ronald Landheer-Cieslak posted:
JKop wrote:
Xenos posted:

Instead of giving the caller write access to the string, I would
suggest "going the way" to fix it. Meaning, keep the member function
constant, and change the return type to const std::string&


I was just trying to get the code to compile without interferring. Good
points though. But then some may say that you should use an accessor
function and return by value... which is a good point also.

-JKop

returning by value implies a copy (which the compiler may then optimize
away) whereas returning a const reference doesn't (so there's less to do
for the optimizer). Hence

const std::string & MyClass::GetTex t(void) const;

is preferable to

std::string MyClass::GetTex t(void) const;

because it doesn't rely on the compiler to optimize the extra copy away.

Note, however, that it does rely on the type of text in the class not
changing: passing back a value is better if you think the type of your
text member variable may change from std::string to something else..

HTH

rlc


But then Implementation must store the string in a fully-fledged string
object. Consider if it pulled if from the air, from the Windows registry,
from the DOS autoexec.bat, then it wouldn't have a reference to return.

-JKop
Jul 22 '05 #9
JKop <NU**@NULL.NULL > wrote in message news:<Ax******* **********@news .indigo.ie>...
I tried fiddling with the code. Here's what I've got:

class Interface
{
public:
virtual std::string& GetText() const = 0;
};

class Implementation : public Interface
{
private:

std::string text;

public:

Implementation( const std::string& in_text) : text(in_text) { ; }

virtual std::string& GetText() const
{
return text;
}
};


I'm curious: Why did you (and E. Robert Tisdale) change my
std::string &getText() const{return text;}
to
virtual std::string &getText() const{return text;}

Is it a convention that in subclasses, the "virtual" keyword should
remain, like in the superclass definition?

Thanks
Markus
Jul 22 '05 #10

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

Similar topics

2
1914
by: lkrubner | last post by:
This is a general computer question, but I'm writing in PHP so I'll post this to comp.lang.php. I've been writing a content management system. I've a Singleton object that keeps track of all errors and stores them in an array. As things work right now, I write out each error message individually. I'm thinking that as the code grows, this system will not continue to scale. Right now my software consists of 1.4 megs of PHP code. I don't...
67
4286
by: Steven T. Hatton | last post by:
Some people have suggested the desire for code completion and refined edit-time error detection are an indication of incompetence on the part of the programmer who wants such features. Unfortunately these ad hominem rhetorts are frequently introduced into purely technical discussions on the feasibility of supporting such functionality in C++. That usually serves to divert the discussion from the technical subject to a discussion of the...
10
5804
by: DataBard007 | last post by:
Hello Access Gurus: I use Win98SE and Access97. I just built a simple Access97 application which holds all contact information for my personal contacts, such as first name, last name, address, city, state, etc. When the user wants to search for a particular record, he does two things: 1. On the form is a text box on which he enters the text he is searching for.
10
2728
by: Brian Conway | last post by:
I have no idea what is going on. I have a Login screen where someone types in their login information and this populates a datagrid based off of the login. Works great in debug and test through VS, however, when I change to release and put it out on the web it fails giving me the following error message The underlying connection was closed. Could not establish a trust relationship with the remote server.
8
9581
by: Brian Tkatch | last post by:
Server: DB2/SUN 8.1.6 Client: DB2 Connect Personal Edition (No 11) <URL:ftp://ftp.software.ibm.com/ps/products/db2/fixes2/english-us/db2winIA32v8/fixpak/FP11_WR21365/FP11_WR21365_CONPE.exe> Uninstalled old version, installed new version, and am now trying to use the CLP. <<<<<<<<<<<< For more detailed help, refer to the Online Reference Manual.
16
2638
by: lawrence k | last post by:
I've made it habit to check all returns in my code, and usually, on most projects, I'll have an error function that reports error messages to some central location. I recently worked on a project where someone suggested to me I was spending too much time writing error messages, and that I was therefore missing the benefit of using a scripting language. The idea, apparently, is that the PHP interpreter writes all the error messages that are...
2
19496
hyperpau
by: hyperpau | last post by:
Before anything else, I am not a very technical expert when it comes to VBA coding. I learned most of what I know by the excellent Access/VBA forum from bytes.com (formerly thescripts.com). Ergo, I will be writing this article intended for those who are in the same level, or maybe lower, of my technical knowledge. I would be using layman's words, or maybe, my own words as how I understand them, hoping, you will understand it the same way that...
2
1878
by: FutureShock | last post by:
I am using a registration class to process a registration form and need some opinions on returning error messages. I am self referring the page on submit. I would like to send each form field to the class for processing (validating, sanitizing, etc..) So far no problem. Now I am throwing ideas around on how best to check for error messages to the users.
0
2897
hyperpau
by: hyperpau | last post by:
Before anything else, I am not a very technical expert when it comes to VBA coding. I learned most of what I know by the excellent Access/VBA forum from bytes.com (formerly thescripts.com). Ergo, I will be writing this article intended for those who are in the same level, or maybe lower, of my technical knowledge. I would be using layman's words, or maybe, my own words as how I understand them, hoping, you will understand it the same way that...
0
10490
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10259
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10238
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7570
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6809
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5467
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5589
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3761
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2941
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.