我正在用 C++ 中的对象进行试验,但遇到了分段错误。里面没有我创建的指针。所以我不知道出了什么问题。
#include <iostream>
#include <iomanip>
#include <locale>
#include <sstream>
#include <string>
using namespace std;
class test {
public:
test(string hw, int tree, string vate);
~test() {
}
string helloworld;
int three;
string privacy() {
cout << "I will now access the private variable PRI\n";
cout << "pri = private\n";
cout << "The value of PRI is "+pri+"\n";
}
private:
string pri;
};
test::test (string hw, int tree, string vate) {
helloworld = hw;
three = tree;
pri = vate;
}
int main() {
cout << "We will now perform an object test!\n";
test thing ("hello world", 3, "private");
cout << "thing.helloworld\n";
cout << "hello world\n";
cout << "Real value: "+thing.helloworld+"\n";
cout << "thing.three\n";
cout << "3\n";
ostringstream thee;
thee << thing.three;
string result = thee.str();
cout << "Real value: "+result+"\n";
cout << "thing.privacy()\n";
cout << "Real value:\n";
thing.privacy();
return 0;
}
这是我得到的错误:
We will now perform an object test!
thing.helloworld
hello world
Real value: hello world
thing.three
3
Real value: 3
thing.privacy()
Real value:
I will now access the private variable PRI
pri = private
The value of PRI is private
*** glibc detected *** ./objecttest: free(): invalid pointer: 0xbfd0d8ac ***
======= Backtrace: =========
/lib/i386-linux-gnu/i686/cmov/libc.so.6(+0x70f01)[0xb7568f01]
/lib/i386-linux-gnu/i686/cmov/libc.so.6(+0x72768)[0xb756a768]
/lib/i386-linux-gnu/i686/cmov/libc.so.6(cfree+0x6d)[0xb756d81d]
/usr/lib/i386-linux-gnu/libstdc++.so.6(_ZdlPv+0x1f)[0xb76ec4bf]
/lib/i386-linux-gnu/i686/cmov/libc.so.6(__libc_start_main+0xe6)[0xb750ee46]
./objecttest[0x8048bb1]
======= Memory map: ========
08048000-0804a000 r-xp 00000000 08:01 782387 /home/jacob/Coding/cpptests/objecttest
0804a000-0804b000 rw-p 00001000 08:01 782387 /home/jacob/Coding/cpptests/objecttest
08098000-080b9000 rw-p 00000000 00:00 0 [heap]
b7300000-b7321000 rw-p 00000000 00:00 0
b7321000-b7400000 ---p 00000000 00:00 0
b74f6000-b74f8000 rw-p 00000000 00:00 0
b74f8000-b7654000 r-xp 00000000 08:01 655678 /lib/i386-linux-gnu/i686/cmov/libc- 2.13.so
b7654000-b7655000 ---p 0015c000 08:01 655678 /lib/i386-linux-gnu/i686/cmov/libc-2.13.so
b7655000-b7657000 r--p 0015c000 08:01 655678 /lib/i386-linux-gnu/i686/cmov/libc-2.13.so
b7657000-b7658000 rw-p 0015e000 08:01 655678 /lib/i386-linux-gnu/i686/cmov/libc-2.13.so
b7658000-b765b000 rw-p 00000000 00:00 0
b765b000-b7677000 r-xp 00000000 08:01 651524 /lib/i386-linux-gnu/libgcc_s.so.1
b7677000-b7678000 rw-p 0001b000 08:01 651524 /lib/i386-linux-gnu/libgcc_s.so.1
b7678000-b7679000 rw-p 00000000 00:00 0
b7679000-b769d000 r-xp 00000000 08:01 655675 /lib/i386-linux-gnu/i686/cmov/libm-2.13.so
b769d000-b769e000 r--p 00023000 08:01 655675 /lib/i386-linux-gnu/i686/cmov/libm-2.13.so
b769e000-b769f000 rw-p 00024000 08:01 655675 /lib/i386-linux-gnu/i686/cmov/libm-2.13.so
b769f000-b777f000 r-xp 00000000 08:01 1046437 /usr/lib/i386-linux-gnu/libstdc++.so.6.0.17
b777f000-b7783000 r--p 000e0000 08:01 1046437 /usr/lib/i386-linux-gnu/libstdc++.so.6.0.17
b7783000-b7784000 rw-p 000e4000 08:01 1046437 /usr/lib/i386-linux-gnu/libstdc++.so.6.0.17
b7784000-b778b000 rw-p 00000000 00:00 0
b77a0000-b77a3000 rw-p 00000000 00:00 0
b77a3000-b77a4000 r-xp 00000000 00:00 0 [vdso]
b77a4000-b77c0000 r-xp 00000000 08:01 651542 /lib/i386-linux-gnu/ld-2.13.so
b77c0000-b77c1000 r--p 0001b000 08:01 651542 /lib/i386-linux-gnu/ld-2.13.so
b77c1000-b77c2000 rw-p 0001c000 08:01 651542 /lib/i386-linux-gnu/ld-2.13.so
bfcee000-bfd0f000 rw-p 00000000 00:00 0 [stack]
Aborted
指针 0xbfd0d8ac 指向什么?