-
April 6th, 2004, 08:56 AM
#1
Compiler error for s.clear()
Consider the following code:
Code:
string dup(char c, int n) {
string s;
s.clear();
s.insert(s.begin(), n, c);
return s; }
My compiler is: Microsoft Visual C++ 6.0
The compiler error I receive is:
h:\c++\proj12ateame\basicutils.h(32) : error C2039: 'clear' : is not a member of 'basic_string<char,struct std::char_traits<char>,class std::allocator<char> >'
This code works FINE on Microsoft Visual Studio .NET 2003, yet it doesn't on Visual C++ 6.0? If someone could explain to me what's going on, I would be grateful.
The header files I have included with this code are <string> and <math.h>
Thanks again guys. You're very helpful.
-
April 6th, 2004, 09:08 AM
#2
Code:
std::string dup(char c, int n)
{
return std::string(n, c);
}
-
April 6th, 2004, 09:09 AM
#3
1. clear() doesn't seem to be a member of std::basic_string, does it?
2. you don't need something like clear() there, as the string is empty.
-
April 6th, 2004, 09:13 AM
#4
However it is strange, I have SGI-STL docs, there is a clear() member in their implementation...
-
April 6th, 2004, 09:50 AM
#5
Originally posted by AvDav
However it is strange, I have SGI-STL docs, there is a clear() member in their implementation...
I didn't see it in the Dinkumware implementation, nor did I find it in the C++ Standard...
-
April 6th, 2004, 10:14 AM
#6
Yes, it's not in the standard, but STLPort includes it. I'm wondering why it isn't in the standard though, since it's potentially a useful function.
Get this small utility to do basic syntax highlighting in vBulletin forums (like Codeguru) easily.
Supports C++ and VB out of the box, but can be configured for other languages.
-
April 6th, 2004, 11:00 AM
#7
Actually, clear() is in the standard ( 21.3.3 , para 12)
With the "effects: behaves as if the function calls"
Code:
erase( begin() , end() );
-
April 6th, 2004, 12:00 PM
#8
Interesting, that's true. The thing is that it's not listed in the public interface for basic_string nor in the index, which is a bit strange. Maybe this is already addressed in a defect report somewhere.
Get this small utility to do basic syntax highlighting in vBulletin forums (like Codeguru) easily.
Supports C++ and VB out of the box, but can be configured for other languages.
-
April 6th, 2004, 12:05 PM
#9
I noticed some people specified Std:: in front of it, but this isn't needed in my case because at the top of the header file that contains this function I used namspace STD.
I hope someone can help me resolve this issue. I haven't really been able to understand the discussion above, about the SGI-STL and Dinkumware lol
Thanks again.
-
April 6th, 2004, 12:13 PM
#10
AvDav gave you the solution already and the rest of the discussion is about the error you get when calling std::string::clear, which doesn't seem to exist in your STL implementation.
Get this small utility to do basic syntax highlighting in vBulletin forums (like Codeguru) easily.
Supports C++ and VB out of the box, but can be configured for other languages.
-
April 6th, 2004, 12:22 PM
#11
Originally posted by MrDoomMaster (bold added by me)
I noticed some people specified Std:: in front of it, but this isn't needed in my case because at the top of the header file that contains this function I used namspace STD.
You shouldn't put "using namespace whatever;" in any header file. Doing that will result in that namespace being used by all files that include your header. Something that in turn can result in name conflicts, since the user of the header doesn't know that the namespace is used.
If you don't want to write std:: for every function/container/etc you use, put all your code in the cpp file (except the declarations of course), and put using namespace at the top of that instead.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|