Re: error with std::string in header file using VS C++ 6.0 SP6

From: Gerhard Menzl <gerhard.menzl_at_spambucket.net>
Date: 3 Dec 2004 09:35:27 -0500


Francis wrote:

> Can anybody help me with this code ? I contains errors I don't understand.
> I use Visual Studio C++ 6.0, Service Pack 6.
>
> CODE (header file)
> ====
> #ifndef framing_h
> #define framing_h
>
> #include <vector>
> #include <string>
>
> std::string::size_type width (const std::vector<string>& v);
> std::vector<string> frame (const vector<string>& v);
> std::vector<string> vcat (const vector<string>& top, const vector<string>& bottom);
> std::vector<string> hcat (const vector<string>& left, const vector<string>& right);
>
> #endif
>
>
> ERROR REPORT
> ============
> error C2065: 'string' : undeclared identifier
> error C2143: syntax error : missing ',' before '<'
> error C2059: syntax error : '<'

You have omitted the std:: qualifier in numerous places before string and vector. If you change the above to

    std::string::size_type width (const std::vector<std::string>& v);
    std::vector<std::string> frame (const std::vector<std::string>& v);
    std::vector<std::string> vcat (const std::vector<std::string>& top, 

                                const std::vector<std::string>& bottom);
    std::vector<std::string> hcat (const std::vector<std::string>& left,
                                const std::vector<std::string>& right);

the errors will disappear. If you think this is cumbersome, you are right. One way around this is to use using declarations:

    using std::string;
    using std::vector;

This allows you to drop all std:: qualifications in the above code. The downside of it is that you import the names string and vector into the global namespace for all clients that include your header. Better still is to use typedefs:

    typedef std::string::sizetype text_size;     typedef std::vector<std::string> text_collection;

These names are just suggestions; use whatever makes sense in your context. Not only does this reduce the clutter considerably:

    text_size width (const text_collection& v);
    text_collection frame (const text_collection& v);
    text_collection vcat (const text_collection& top,
                          const text_collection& bottom);
    text_collection hcat (const text_collection& left,
                          const text_collection& right);

it also makes it much easier to change the underlying container if need arises.

-- 
Gerhard Menzl

#dogma int main ()

Humans may reply by replacing the obviously faked part of my e-mail 
address with "kapsch".

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
Received on Fri Dec 03 2004 - 06:31:47 PST

Click to report inappropriate content