#include<iostream>
#include<cstring>
usingnamespace std;
class Record {
public:
typedef size_t size;
Record() {bytecount = 0;}
Record(size s) {bytecount=s;}
Record(string ss) {name = ss; bytecount = 0;}
size getbyte() const {cout<<"The bytecount is "<<bytecount<<endl;}
string getname() const {cout<<"The name is "<<name<<endl;}
private:
size bytecount;
string name;
};
int main()
{
Record A("Tr");
A.getname();
system("pause");
}
When I run this little program, it succeed but also a warning window saying "application error, the memory 0XXXXX cannot be written"(I rephrased it). What is wrong and how to solve this problem?
Thanks
"memory at xxx cannot be read/written" errors are typically caused by trying to dereference a bad pointer. Although I don't see any of that here, so that is a little puzzling.
What I do see is that getbyte() and getname() both have return value (size and string respectively), but you're not returning anything from either of them. I'm surprised your compiler let you do this -- it really should have errored on you. Either make those functions void, or return a value from them.
Same thing with main() (although compilers usually don't complain about that, which I never understood). Throw a return 0; at the end of it.
Try changing those things and see if it helps. I don't see anything else wrong here.
edit: except.. using std::string but not including <string>? This program compiled?
This code wouldn't actually compile with the Visual C++ 2005 compiler. Here are a couple changes I made to it that caused it to compile and run fine for me. If this doesn't work I'd guess it was your compiler/OS like jsmith said.