错误4430:缺少类型说明符 - 假定为int。注意:C ++不支持default-int

时间:2015-04-17 23:14:52

标签: c++ string class

fndef FIXED_LENGTH_STRING_H
#define FIXED_LENGTH_STRING_H

#include <iostream>

using namespace std;

#include <string.h>

template <int Length>
class FixedLengthString
    {
    public:
        enum Exceptions {InvalidLength};
                                        FixedLengthString       ();
                                        FixedLengthString       (const FixedLengthString <Length> &);
                                        FixedLengthString       (const char []);
                                        ~FixedLengthString      ();
                                        Copy                    (const FixedLengthString <Length> &);
                                        Copy                    (const char []);
        istream &                       Read                    ();
        FixedLengthString <Length> &    operator =              (const FixedLengthString <Length> &);
        FixedLengthString <Length> &    operator =              (const char []);
        bool                            operator <              (const FixedLengthString <Length> &) const;
        bool                            operator <              (const char []) const;
                                            // also need the other comparison operators
                                        operator const char *   () const;
    private:
        char    Data [Length + 1];
    };

template <int Length>
ostream & operator << (ostream & out, const FixedLengthString <Length> & Str)
    {
    // display the characters in the string
    return out;
    }

template <int Length>
istream & operator >> (istream & in, FixedLengthString <Length> & Str)
    {
    Str.Read ();
    return in;
    }

template <int Length>
FixedLengthString <Length>::FixedLengthString (const char S [])
    {
    if (Length != strlen (S))
            throw InvalidLength;
        else
            strcpy (Data, S);
    }

template <int Length>
inline FixedLengthString <Length>::operator const char * () const
    {
    return Data;
    }

#endif

编译器指向两个Copy构造函数。我正在创建一个模板,其中包含字符数作为模板参数。从这里我需要一个固定长度的字符串

在我的FixedLengthString.cpp文件中,我有这个

FixedLengthString<Length>::Copy(const FixedLengthString <Length> & M)
{
strcpy(Data, M.Data)
}

template <int Length>
FixedLengthString<Length>::Copy(const char M [])
{
strcpy(Data, M.Data)
}

1 个答案:

答案 0 :(得分:1)

您只有一个复制构造函数,因为只能有一个。

但是你忘记了返回类型 - void看似合理 - 在你的两个名为“复制”的函数上,所以我怀疑你指的是那些。
但它们不是构造函数,只是常规函数。

并且您不能在单独的文件中使用模板函数实现,您必须将它们放在标题中。

相关问题