I copied this code from a tutorial to play around with. However, I kept on getting an error that stated that I can't have any empty character constants. The tutorial was in Visual Studio 2008, and I am using Visual Studio 2013, so perhaps this is no longer valid, but I can't find any fix.
Here is the code:
#include "stdafx.h"
#include <iostream>
class MyString
{
private:
char *m_pchString;
int m_nLength;
public:
MyString(const char *pchString="")
{
// Find the length of the string
// Plus one character for a terminator
m_nLength = strlen(pchString) + 1;
// Allocate a buffer equal to this length
m_pchString = new char[m_nLength];
// Copy the parameter into our internal buffer
strncpy(m_pchString, pchString, m_nLength);
// Make sure the string is terminated
// this is where the error occurs
m_pchString[m_nLength-1] = '';
}
~MyString() // Destructor
{
// We need to deallocate our buffer
delete[] m_pchString;
// Set m_pchString to null just in case
m_pchString = 0;
}
char* GetString() { return m_pchString; }
int GetLength() { return m_nLength; }
};
int main()
{
MyString cMyName("Alex");
std::cout << "My name is: " << cMyName.GetString() << std::endl;
return 0;
}
The error I get is the following:
Error 1 error C2137: empty character constant