Connecting Tech Pros Worldwide Forums | Help | Site Map

string class won't compile

Richard Cavell
Guest
 
Posts: n/a
#1: Jul 22 '05
#include <string>
using std::string;
#include <stdio.h>
#include <stdlib.h>
#include <gmp.h>

void My Function(string ParameterOne, string ParameterTwo)
{
int i, j;
string MyString = "hello";
}


This chokes on the last line and says "error: 'string' undeclared (first
use in this function). What am I doing wrong?

JKop
Guest
 
Posts: n/a
#2: Jul 22 '05

re: string class won't compile


Made 3 changes:

1) Got rid of #include <gmp.h>

2) Fixed typo in function definition

3) Put in "main".


The following should and does compile:


#include <string>

using std::string;

#include <stdio.h>
#include <stdlib.h>

//#include <gmp.h>

void MyFunction(string ParameterOne, string ParameterTwo)
{
int i, j;
string MyString = "hello";
}

int main()
{

}


-JKop

Andre Kostur
Guest
 
Posts: n/a
#3: Jul 22 '05

re: string class won't compile


JKop <NULL@NULL.NULL> wrote in news:1EfLc.5079$Z14.6129@news.indigo.ie:
[color=blue]
> Made 3 changes:
>
> 1) Got rid of #include <gmp.h>
>
> 2) Fixed typo in function definition
>
> 3) Put in "main".
>
>
> The following should and does compile:
>
>
> #include <string>
>
> using std::string;
>
> #include <stdio.h>
> #include <stdlib.h>
>
> //#include <gmp.h>[/color]

Stylistic point to consider....

I'd move all of your using declarations after _all_ includes. If you
don't, you may inadvertantly bring in a bad symbol lookup (or
ambiguity)....
Miro Jurisic
Guest
 
Posts: n/a
#4: Jul 22 '05

re: string class won't compile


In article <Xns952C93265BC62nntpspamkosturnet@207.35.177.135> ,
Andre Kostur <nntpspam@kostur.net> wrote:
[color=blue]
> JKop <NULL@NULL.NULL> wrote in news:1EfLc.5079$Z14.6129@news.indigo.ie:
>[color=green]
> > Made 3 changes:
> >
> > 1) Got rid of #include <gmp.h>
> >
> > 2) Fixed typo in function definition
> >
> > 3) Put in "main".
> >
> >
> > The following should and does compile:
> >
> >
> > #include <string>
> >
> > using std::string;
> >
> > #include <stdio.h>
> > #include <stdlib.h>
> >
> > //#include <gmp.h>[/color]
>
> Stylistic point to consider....
>
> I'd move all of your using declarations after _all_ includes. If you
> don't, you may inadvertantly bring in a bad symbol lookup (or
> ambiguity)....[/color]

I disagree. I find that

#include <string>
using std::string

#include <vector>
using std::vector

#include <...>
using ...

much much more legible than what you propose, and the risk of ambiguity is low
(so low, in fact, that I have never run into this problem, and I use STL and
boost heavily).

meeroh

--
If this message helped you, consider buying an item
from my wish list: <http://web.meeroh.org/wishlist>

Mark
Guest
 
Posts: n/a
#5: Jul 22 '05

re: string class won't compile



[...][color=blue][color=green]
>>
>> The following should and does compile:
>>
>>
>> #include <string>
>>
>> using std::string;
>>
>> #include <stdio.h>
>> #include <stdlib.h>
>>
>> //#include <gmp.h>[/color]
>
>Stylistic point to consider....
>
>I'd move all of your using declarations after _all_ includes. If you
>don't, you may inadvertantly bring in a bad symbol lookup (or
>ambiguity)....[/color]

Bad symbol lookup or ambiguity. Sure? Interesting!!!

Mark
--
[ C++ FAQ: http://www.parashift.com/c++-faq-lite/ ]

David Hilsee
Guest
 
Posts: n/a
#6: Jul 22 '05

re: string class won't compile


"Miro Jurisic" <macdev@meeroh.org> wrote in message
news:macdev-84BBD9.17554820072004@senator-bedfellow.mit.edu...[color=blue]
> I disagree. I find that
>
> #include <string>
> using std::string
>
> #include <vector>
> using std::vector
>
> #include <...>
> using ...
>
> much much more legible than what you propose, and the risk of ambiguity is[/color]
low[color=blue]
> (so low, in fact, that I have never run into this problem, and I use STL[/color]
and[color=blue]
> boost heavily).[/color]

Beauty is in the eye of the beholder. I prefer this order: application
headers, standard headers, using declarations/directives. The elimination
of potential ambiguities/hidden dependencies is an added bonus.

--
David Hilsee


Andre Kostur
Guest
 
Posts: n/a
#7: Jul 22 '05

re: string class won't compile


Mark <mark_netNews@hotmail.com> wrote in
news:jrlrf0hh21542vt088qfbdanjsv1otkatm@4ax.com:
[color=blue]
>
> [...][color=green][color=darkred]
>>>
>>> The following should and does compile:
>>>
>>>
>>> #include <string>
>>>
>>> using std::string;
>>>
>>> #include <stdio.h>
>>> #include <stdlib.h>
>>>
>>> //#include <gmp.h>[/color]
>>
>>Stylistic point to consider....
>>
>>I'd move all of your using declarations after _all_ includes. If you
>>don't, you may inadvertantly bring in a bad symbol lookup (or
>>ambiguity)....[/color]
>
> Bad symbol lookup or ambiguity. Sure? Interesting!!![/color]

If you have an unqualified identifier in a subsequent header file, which
namespace does it come from? Let's assume:

/// a.h

extern void fn(string str);

/// a.cpp

#include <string>
using std::string;

#include "a.h"

void fn(string str)
{
}

/// b.cpp

#include <customstring>
using customstring::string;

#include "a.h"

void bfn()
{
fn("");
}



What happens in b.cpp?
Mark
Guest
 
Posts: n/a
#8: Jul 22 '05

re: string class won't compile


On Wed, 21 Jul 2004 15:43:33 GMT, Andre Kostur <nntpspam@kostur.net>
wrote:

[...]
[color=blue][color=green]
>> Bad symbol lookup or ambiguity. Sure? Interesting!!![/color]
>
>If you have an unqualified identifier in a subsequent header file, which
>namespace does it come from? Let's assume:
>
>/// a.h
>
>extern void fn(string str);
>
>/// a.cpp
>
>#include <string>
>using std::string;
>
>#include "a.h"
>
>void fn(string str)
>{
>}
>
>/// b.cpp
>
>#include <customstring>
>using customstring::string;
>
>#include "a.h"
>
>void bfn()
>{
> fn("");
>}
>
>
>
>What happens in b.cpp?[/color]

Point taken. As I understand the example, it boils down to which
namespace, hence the ambiguity.

Mark
--
[ C++ FAQ: http://www.parashift.com/c++-faq-lite/ ]

Closed Thread