Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   New to C++ (http://www.velocityreviews.com/forums/t955600-new-to-c.html)

Pinnerite 12-17-2012 07:05 PM

New to C++
 
I'm actually trying to get back in after many years stuck in the past.
Below is code from a test routine intended to solve my technical mistakes:

#include <iostream>
#include <string>

using namespace std;

int main(int argc, char *argv[])
{
string myfilename = "";
string getfilename(); // error message 1

string getfilename(myfilename); // error message 2

return 0;
}

string getfilename(string myfilename)
{
myfilename = "success";

return myfilename;
}


Error messages
1) std::string getfilename' redeclared as different kind of symbol
2) error: previous declaration of 'std::string getfilename()'


I would be grateful for corrections. My tutorial doesn't seem to cover it.




--
__________________________________________________ _

Mageia 2 for x86_64, Kernel: 3.4.13-desktop-1.mga2
KDE version 4.8.4 Running on an AMD 4-core processor


Stuart 12-17-2012 09:24 PM

Re: New to C++
 
On 12/17/12 Pinnerite wrote:
> I'm actually trying to get back in after many years stuck in the past.
> Below is code from a test routine intended to solve my technical mistakes:
>
> #include <iostream>
> #include <string>
>
> using namespace std;
>
> int main(int argc, char *argv[])
> {
> string myfilename = "";
> string getfilename(); // error message 1
>
> string getfilename(myfilename); // error message 2
>
> return 0;
> }
>
> string getfilename(string myfilename)
> {
> myfilename = "success";
>
> return myfilename;
> }
>
>
> Error messages
> 1) std::string getfilename' redeclared as different kind of symbol
> 2) error: previous declaration of 'std::string getfilename()'
>
>
> I would be grateful for corrections. My tutorial doesn't seem to cover it.
>


Just to add what others have already posted: I think that this kind of
trap could be easily avoided with a better error description. The line
that is indicated by "// error message 1" is almost always a mistake. I
have never ever seen anyone using such a thing in my ten years of
prefessional C++ programming. On the other hand, I have seen lots of
people that fell for this.

So in my humple opinion the error message 1 should be something "The
function declaration 'string getfilename();' is redeclaring <something>.
Did you really mean to declare a function?"

Regards,
Stuart


Victor Bazarov 12-17-2012 09:53 PM

Re: New to C++
 
On 12/17/2012 4:24 PM, Stuart wrote:
> On 12/17/12 Pinnerite wrote:
>> I'm actually trying to get back in after many years stuck in the past.
>> Below is code from a test routine intended to solve my technical
>> mistakes:
>>
>> #include <iostream>
>> #include <string>
>>
>> using namespace std;
>>
>> int main(int argc, char *argv[])
>> {
>> string myfilename = "";
>> string getfilename(); // error message 1
>>
>> string getfilename(myfilename); // error message 2
>>
>> return 0;
>> }
>>
>> string getfilename(string myfilename)
>> {
>> myfilename = "success";
>>
>> return myfilename;
>> }
>>
>>
>> Error messages
>> 1) std::string getfilename' redeclared as different kind of symbol
>> 2) error: previous declaration of 'std::string getfilename()'
>>
>>
>> I would be grateful for corrections. My tutorial doesn't seem to cover
>> it.
>>

>
> Just to add what others have already posted: I think that this kind of
> trap could be easily avoided with a better error description. The line
> that is indicated by "// error message 1" is almost always a mistake. I
> have never ever seen anyone using such a thing in my ten years of
> prefessional C++ programming. On the other hand, I have seen lots of
> people that fell for this.


You seem to miss a simple possibility. It's a function declaration, but
is without any arguments specified - like C90 (and K&R C) used to do.
It means "any arguments are OK".

In C++ empty parens means "no arguments shall be given". That's the
main difference here.

> So in my humple opinion the error message 1 should be something "The
> function declaration 'string getfilename();' is redeclaring <something>.
> Did you really mean to declare a function?"


I think that's exactly what OP did (or rather intended to do). Old C
habits die hard.

V
--
I do not respond to top-posted replies, please don't ask

Pinnerite 12-18-2012 12:06 PM

Re: New to C++
 
Juha Nieminen wrote:

> Pinnerite <pinnerite@gmail.com> wrote:
>> using namespace std;

>
> I'm curious to know where you learned that line and why you are using it.
>
> --- news://freenews.netfront.net/ - complaints: news@netfront.net ---



Page 63 of C++ Programming, 3rd edition by Mike McGrath; and
page 7 of 'The C++ Language Tutorial' from
http://www.cplusplus.com/doc/tutorial/



--
__________________________________________________ _

Mageia 2 for x86_64, Kernel: 3.4.13-desktop-1.mga2
KDE version 4.8.4 Running on an AMD 4-core processor


Pinnerite 12-18-2012 12:29 PM

Re: New to C++
 
Stuart wrote:

> On 12/17/12 Pinnerite wrote:
>> I'm actually trying to get back in after many years stuck in the past.
>> Below is code from a test routine intended to solve my technical
>> mistakes:
>>
>> #include <iostream>
>> #include <string>
>>
>> using namespace std;
>>
>> int main(int argc, char *argv[])
>> {
>> string myfilename = "";
>> string getfilename(); // error message 1
>>
>> string getfilename(myfilename); // error message 2
>>
>> return 0;
>> }
>>
>> string getfilename(string myfilename)
>> {
>> myfilename = "success";
>>
>> return myfilename;
>> }
>>
>>
>> Error messages
>> 1) std::string getfilename' redeclared as different kind of symbol
>> 2) error: previous declaration of 'std::string getfilename()'
>>
>>
>> I would be grateful for corrections. My tutorial doesn't seem to cover
>> it.
>>

>
> Just to add what others have already posted: I think that this kind of
> trap could be easily avoided with a better error description. The line
> that is indicated by "// error message 1" is almost always a mistake. I
> have never ever seen anyone using such a thing in my ten years of
> prefessional C++ programming. On the other hand, I have seen lots of
> people that fell for this.
>
> So in my humple opinion the error message 1 should be something "The
> function declaration 'string getfilename();' is redeclaring <something>.
> Did you really mean to declare a function?"
>
> Regards,
> Stuart


Thank you for all your advice. It is at least 25 years since I touched 'C'
but I have continued to maintain a program written in Clipper started over
20 years ago, that uses 'C' like structures and presentation. Accordingly I
am wary of making assumptions.

The routine is being tested in the QT Creator IDE and though the error
messages seem arcane to the inexperienced eye, I can see that they are
beginning to make sense.

Please excuse me though if I come back with yet another bloomer. It will not
be for want of trying to find solutions myself.

Interestingly Mike McGrath's book doesn't show a single illustration of one
function calling another.

Regards

Alan



--
__________________________________________________ _

Mageia 2 for x86_64, Kernel: 3.4.13-desktop-1.mga2
KDE version 4.8.4 Running on an AMD 4-core processor


Stuart 12-19-2012 10:15 PM

Re: New to C++
 
On 12/17/2012 Stuart wrote:
[snip]
>> Just to add what others have already posted: I think that this kind of
>> trap could be easily avoided with a better error description. The line
>> that is indicated by "// error message 1" is almost always a mistake. I
>> have never ever seen anyone using such a thing in my ten years of
>> prefessional C++ programming. On the other hand, I have seen lots of
>> people that fell for this.



On 12/17/12 Victor Bazarov wrote:
> You seem to miss a simple possibility. It's a function declaration, but
> is without any arguments specified - like C90 (and K&R C) used to do. It
> means "any arguments are OK".
>
> In C++ empty parens means "no arguments shall be given". That's the
> main difference here.
>
>> So in my humple opinion the error message 1 should be something "The
>> function declaration 'string getfilename();' is redeclaring <something>.
>> Did you really mean to declare a function?"

>
> I think that's exactly what OP did (or rather intended to do). Old C
> habits die hard.


I'm so happy that I was born so late that I never had to have use C.
OTOH, I am also happy that I wasn't born too late so that the only thing
I could learn at university was Java, either. Apparently I have to
consider myself quite a lucky guy ...

Stuart


Rui Maciel 12-20-2012 07:35 PM

Re: New to C++
 
Stuart wrote:

> I'm so happy that I was born so late that I never had to have use C.
> OTOH, I am also happy that I wasn't born too late so that the only thing
> I could learn at university was Java, either. Apparently I have to
> consider myself quite a lucky guy ...


C isn't as bad as you made it out to be. In fact, it's actually quite nice,
particularly when considering how other languages, such as C++, ended up
being so complex and hard to master. It might not be perfect, but what is?


Rui Maciel

Swifty 12-21-2012 08:34 AM

Re: New to C++
 
On 19/12/2012 22:22, Scott Lurndal wrote:
> Consider yourself lucky you never had to use COBOL


Here in the UK, I was taught that this is one of the reasons for the
National Lottery, as demonstrated in the proforma resignation letter:

"... I won the Jackpot on the National Lottery, so you can stuff your
COBOL programs..."

Such dreams were commonplace during late 1999 when the COBOL programmers
were checking for Y2K bugs.

P.S. I only ever found one Y2K bug in my own code, so I changed it into
a Y3K bug as the simplest solution.

P.P.S One of my colleagues actually did win the jackpot, and a rollover
to boot. His VoiceMail message said simply "Leave a message after the
tone; I'll never get back to you. I won Saturday's rollover jackpot, and
by the time you hear this, I'll be in the Bahamas"

--
Steve Swift
http://www.swiftys.org.uk/

Paul Rubin 12-28-2012 08:56 AM

Re: New to C++
 
Juha Nieminen <nospam@thanks.invalid> writes:
>> Page 63 of C++ Programming, 3rd edition by Mike McGrath; and
>> page 7 of 'The C++ Language Tutorial' from
>> http://www.cplusplus.com/doc/tutorial/

> I think it's a shame that textbooks and tutorials teach such a bad habit.


See also B. Stroustrup, The C++ Programming Language (special edition),
p. 47. Admittedly he doesn't exactly recommend the practice.

Öö Tiib 12-28-2012 10:36 AM

Re: New to C++
 
On Friday, 28 December 2012 10:45:41 UTC+2, Juha Nieminen wrote:
> Pinnerite <pinnerite@gmail.com> wrote:
> >> I'm curious to know where you learned that line and why you are using it.

> >
> > Page 63 of C++ Programming, 3rd edition by Mike McGrath; and
> > page 7 of 'The C++ Language Tutorial' from
> > http://www.cplusplus.com/doc/tutorial/

>
> I think it's a shame that textbooks and tutorials teach such a bad habit.


Yes. Majority of examples do it. Same with boost. Most boost examples
have using directive in them. Rare (like boost::program_options)
examples use namespace aliases. Next to never you can find using
directive for the thing demonstrated in example.

The authors say (when asked about that practice) that it makes the
examples shorter and so simpler to read. I do not think that they
use using directive in their own works. That is apparently yet
another of the bear traps that our community provides to the poor
novices.



All times are GMT. The time now is 07:23 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.