|
-
January 13th, 2015, 06:30 AM
#3
Re: A simple vmString class
Why not just use the standard c++ string class? http://www.cplusplus.com/reference/string/
You don't forward declare ostream. ostream is part of the namespace std so
Code:
friend std::ostream &operator<<(std::ostream &output, const vmString &str);
and other places where you use ostream.
You need to define the overloaded operator << within the vmStd namespace.
This code compiles and produces the expected output
Code:
#include <iostream>
#include <cstring>
#include <cassert>
namespace vmStd {
class vmString {
friend std::ostream &operator<<(std::ostream &output, const vmString &str);
public:
vmString(const char *str = "");
vmString(const vmString &cpy);
~vmString();
int length() const { return m_nLength; }
private:
void AllocString(const char *str);
int m_nLength;
char *m_pchStr; // error: char* vmStd::vmString::m_pchStr' is private
};
} // namespace vmStd
using namespace vmStd;
//-----------------------------------------------------------------------------
// FN/Method: vmString(const char * = "")
// Description: vmString class constructor. Converts char * to a vmString
// object.
//-----------------------------------------------------------------------------
vmString::vmString(const char *str) : m_nLength(strlen(str)), m_pchStr(0)
{
AllocString(str);
}
//-----------------------------------------------------------------------------
// FN/Method: vmString(const vmString &)
// Description: vmString class copy constructor
//-----------------------------------------------------------------------------
vmString::vmString(const vmString& cpy) : m_nLength(cpy.m_nLength)
{
AllocString(cpy.m_pchStr);
}
//-----------------------------------------------------------------------------
// FN/Method: ~vmString()
// Description: vmString class destructor
//-----------------------------------------------------------------------------
vmString::~vmString()
{
delete[] m_pchStr;
}
//-----------------------------------------------------------------------------
// FN/Method: AllocString(const char *)
// Description: Dynamically allocate memory for new vmString object
//-----------------------------------------------------------------------------
void vmString::AllocString(const char *str)
{
m_pchStr = new char[m_nLength + 1]; // Allocate storage
assert(m_pchStr != 0); // Terminate if memory not allocated
strcpy(m_pchStr, str); // Copy literal to vmString object
}
//-----------------------------------------------------------------------------
// FN/Method: ostream &operator<<(ostream &, const vmString &)
// Description: Allow the output of a vmString object
//-----------------------------------------------------------------------------
namespace vmStd {
std::ostream &operator<<(std::ostream &output, const vmStd::vmString &str)
{
output << str.m_pchStr; // error: within this context ???
return output; // Enables cascading
}
}
int main()
{
using namespace vmStd;
// One part construction
vmString testStr = "Hello string";
std::cout << testStr << std::endl;
return 0;
}
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|