0

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 は何を指していますか?

4

1 に答える 1

5

あなたstring privacy()は a を返しますがstring、何も返しません。値を返す関数で関数の最後からフローするのは、未定義の動作です。おそらくあなたはそれを望んでいましたvoid privacy()

また、 でコンパイルすることをお勧めし-Wallます。[-Wreturn-type]問題をすぐに知らせるコンパイラ警告が表示されます。

警告: 非 void [-Wreturn-type] を返す関数に return ステートメントがありません

于 2013-08-10T21:49:59.597 に答える