A question

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include<iostream>
#include<cstring>
using namespace 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?
Last edited on
iostream typically includes string.

returning a value from main() won't change anything.

there is nothing wrong with your program. it is either your compiler or OS.
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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include<iostream>
#include<string> // Changed from cstring
using namespace std;

class Record {
public:
typedef size_t size;
Record() {bytecount = 0;}
Record(size s) {bytecount=s;}
Record(string ss) {name = ss; bytecount = 0;}
void getbyte() const {cout<<"The bytecount is "<<bytecount<<endl;} // changed to return void
void getname() const {cout<<"The name is "<<name<<endl;} // changed to return void
private:
size  bytecount;
string name;
};

int main()
{
Record A("Tr");
A.getname();
system("pause");
return 0; // Correctly returns 0.
}


Topic archived. No new replies allowed.