0
我做了我自己的字符串類,由於某種原因,intellisense沒有選擇我明確定義和嘗試使用的方法。 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
不承認他們作爲我的字符串類的成員?
發佈一個[SSCCE](http://sscce.org),它演示了錯誤和確切的錯誤。 – chris
剛剛添加了確切的錯誤消息和它打破@chris的行。 – MrPickle5
我無法重現這一點。我得到一個關於沒有C字符串函數的錯誤,因爲你忘記了包含'',但是它乾淨地編譯。這絕對不是SSCCE。 http://coliru.stacked-crooked.com/a/e6ba596a1cf473aa –
chris