【问题标题】:error 4430: missing type specifier - int assumed. Note: C++ does not support default-int [duplicate]错误 4430:缺少类型说明符 - 假定为 int。注意:C++ 不支持默认整数 [重复]
【发布时间】:2015-04-17 23:14:52
【问题描述】:
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)
}

【问题讨论】:

  • 我不会说这是重复的,但@Axalo 是对的;只能在头文件中定义模板。

标签: c++ string class


【解决方案1】:

你只有一个拷贝构造函数,因为只能有一个。

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

而且你不能将模板函数实现放在一个单独的文件中,你必须把它们放在头文件中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-01-28
    • 1970-01-01
    • 2013-09-17
    • 1970-01-01
    • 2020-03-23
    • 1970-01-01
    • 2017-03-28
    相关资源
    最近更新 更多