0

I made my own string class and for some reason, the intellisense is not picking up methods that I clearly defined and am trying to use. Not only is the intellisense not picking up these methods, but I get a build error when I try to compile.

The only method that the intellisense is picking up is Display(). Test() and GetString() are no where to be found in the String class and say There is no member Test() and no member GetString when I try to compile.

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

The exact error occurs at this line with the error message:

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;
}

Does anyone know why the class won't compile?

Does anyone know why the compiler is saying void Test() and char * GetString is not recognizing them as members of my string class??

5
  • 1
    Post an SSCCE that demonstrates the error and the exact error from it. Commented Jan 17, 2014 at 4:14
  • Just added the exact error message and the line that it breaks @chris. Commented Jan 17, 2014 at 4:19
  • I can't reproduce this. I get an error about no C string functions because you forgot to include <cstring>, but then it compiles cleanly. And that's definitely not an SSCCE. coliru.stacked-crooked.com/a/e6ba596a1cf473aa Commented Jan 17, 2014 at 4:21
  • Thanks chris. Yeah, I made a brand new string class, copy and pasted everything and it works now. So weird. :( Commented Jan 17, 2014 at 4:24
  • seem to be linker problem can you show us the all files in the project Commented Jan 17, 2014 at 4:25

1 Answer 1

1

I compiled both string.h & string.cpp on Microsoft Visual C++ 2005 & its not giving me error that you specified.

I replaced nullptr with NULL as I am not using C++11. Also, by replacing strcpy with strcpy_s & it compiled.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.