编译错误和String类无法识别方法

时间:2014-01-17 04:13:54

标签: c++ class compilation

我创建了自己的字符串类,出于某种原因,intellisense没有采用我明确定义并尝试使用的方法。智能感知器不仅没有采用这些方法,而且在我尝试编译时遇到了构建错误。

智能感知的唯一方法是Display()。在Test()班级中找不到GetString()String,在我尝试编译时说There is no member Test() and no member GetString

string.cpp

#include "String.h"
#include <iostream>

using std::cout;
using std::endl;

String::String() :m_SingleChar('\0'), m_CString(nullptr)
{ }

String::String(char Character) : m_SingleChar(Character)
{ }

String::String(char * CharacterString)
{
    if (CharacterString != nullptr)
    {
        m_CString = new char [strlen(CharacterString) + 1];
        strcpy (m_CString, CharacterString);
    }
}

String::~String()
{
    delete [] m_CString;
}

String::String(const String & copy) : m_SingleChar(copy.m_SingleChar)
{
    if (copy.m_CString != nullptr)
    {
        m_CString = new char [strlen(copy.m_CString) + 1];
        strcpy (m_CString, copy.m_CString);
    }
}

String & String::operator = (const String & rhs)
{
    if (this != &rhs)
    {
        delete [] m_CString;
        if(rhs.m_CString == nullptr)
            m_CString = nullptr;
        else
        {
            m_CString = new char[strlen(rhs.m_CString) + 1];
            strcpy (m_CString, rhs.m_CString);
        }
        m_SingleChar = rhs.m_SingleChar;
    }
    return *this;
}

char * String::GetString(String m_CString)
{
    char * Cstring = m_CString.m_CString;
    return Cstring;
}

void String::Test()
{
}

void String::Display()
{
    cout << "Single character: " << m_SingleChar << endl;
    cout << "C String:  " << m_CString << endl;
}

string.h中

#ifndef STRING_H
#define STRING_H


class String
{
public:
    String();
    String(char Character);
    String(char * CharacterString);
    ~String();
    String(const String & copy);
    String & operator = (const String & rhs);
    char * GetString (String CString);
    void Test();
    void Display();

private:
    char m_SingleChar;
    char * m_CString;
};

#endif

此行发生确切错误,并显示错误消息:

Error   1   error C2039: 'Test' : is not a member of 'String'       
23  1   Potion Class     2

...

int * Potion::GetMoney (int * coins)
{
    String something;
    something.Test();  //Error occurs here

    return coins;
}

有谁知道为什么课程不会编译?

有谁知道编译器为什么说void Test()char * GetString没有将它们识别为我的字符串类的成员?

1 个答案:

答案 0 :(得分:1)

我编译了string.h和amp; Microsoft Visual C ++ 2005上的string.cpp&amp;它没有给我你指定的错误。

我将nullptr替换为NULL,因为我没有使用C ++ 11。此外,将strcpy替换为strcpy_s&amp;它汇编了。