我有这个简单的代码,我试图在tutorialspoint.com中运行
#include <iostream>
using namespace std;
class Vehicle {
string vehicleNo;
string color;
protected:
string getVehicleNo()
{
return vehicleNo;
}
string setVehicleNo(string num)
{
vehicleNo = num;
}
string getColor()
{
return color;
}
string setColor(string someColor)
{
color = someColor;
}
};
class Car: public Vehicle {
private:
int distance;
public:
void setDistance(int a)
{
distance = a;
}
int calculateFare(int)
{
return distance * 5;
}
void displayInformation()
{
cout << distance << endl;
}
void useSetVehicleNo(string num)
{
setVehicleNo(num);
}
string useSetColor(string someColor)
{
setColor(someColor);
}
string useGetVehicleNo()
{
return getVehicleNo();
}
string useGetColor()
{
return getColor();
}
};
int main()
{
//string exit=n;
string vehicleNo;
string color;
Car car;
cout << "Enter car number : " << endl;
cin >> vehicleNo;
car.useSetVehicleNo(vehicleNo);
cout << "Enter car color : " << endl;
cin >> color;
car.useSetColor(color);
//vehicleNo = car.useGetColor();
//color = car.useGetVehicleNo();
//cout << vehicleNo;
//cout << color;
return 0;
}
运行它会给我以下输出
输入车号:
1
输入汽车颜色:
g*'main&#39;:free()出错:指针无效:0x00007ff3aa205ba8 *
======= Backtrace:========= /lib64/libc.so.6(+0x7850e)[0x7ff3a969450e]
/lib64/libc.so.6(cfree+0x5b5)[0x7ff3a96a0165]
主[0x400e97]
主[0x400c79]
/lib64/libc.so.6(__libc_start_main+0xf0)[0x7ff3a963bfe0]
main [0x400ac9]=======记忆图:======== 00400000-00402000 r-xp 00000000 fd:11f 59330668
/ home / cg / roo t / main
00601000-00602000 r - p 00001000 fd:11f 59330668
/ home / cg / roo t / main
00602000-00603000 rw-p 00002000 fd:11f 59330668
/ home / cg / roo t / main
0140c000-0142d000 rw-p 00000000 00:00 0
[堆]
7ff3a961c000-7ff3a97d0000 r-xp 00000000 fd:11f 37749826
的/ usr / lib64下/升
ibc-2.20.so
7ff3a97d0000-7ff3a99cf000 --- p 001b4000 fd:11f 37749826
的/ usr / lib64下/升
ibc-2.20.so
7ff3a99cf000-7ff3a99d3000 r - p 001b3000 fd:11f 37749826
的/ usr / lib64下/升
ibc-2.20.so
7ff3a99d3000-7ff3a99d5000 rw-p 001b7000 fd:11f 37749826
的/ usr / lib64下/升
ibc-2.20.so
7ff3a99d5000-7ff3a99d9000 rw-p 00000000 00:00 0
7ff3a99d9000-7ff3a99ef000 r-xp 00000000 fd:11f 37749844
的/ usr / lib64下/升
ibgcc_s-4.9.2-20141101.so.1 7ff3a99ef000-7ff3a9bee000 --- p 00016000 fd:11f 37749844 / usr / lib64 / l
ibgcc_s-4.9.2-20141101.so.1
7ff3a9bee000-7ff3a9bef000 r - p 00015000 fd:11f 37749844
的/ usr / lib64下/升
ibgcc_s-4.9.2-20141101.so.1
7ff3a9bef000-7ff3a9bf0000 rw-p 00016000 fd:11f 37749844
的/ usr / lib64下/升
ibgcc_s-4.9.2-20141101.so.1
7ff3a9bf0000-7ff3a9cf7000 r-xp 00000000 fd:11f 37749280
的/ usr / lib64下/升
ibm-2.20.so
7ff3a9cf7000-7ff3a9ef6000 --- p 00107000 fd:11f 37749280
的/ usr / lib64下/升
ibm-2.20.so
7ff3a9ef6000-7ff3a9ef7000 r - p 00106000 fd:11f 37749280
的/ usr / lib64下/升
ibm-2.20.so
7ff3a9ef7000-7ff3a9ef8000 rw-p 00107000 fd:11f 37749280
的/ usr / lib64下/升
ibm-2.20.so
7ff3a9ef8000-7ff3a9fe8000 r-xp 00000000 fd:11f 37749829
的/ usr / lib64下/升
ibstdc ++。so.6.0.20
7ff3a9fe8000-7ff3aa1e8000 --- p 000f0000 fd:11f 37749829
的/ usr / lib64下/升
ibstdc ++。so.6.0.20
7ff3aa1e8000-7ff3aa1f0000 r - p 000f0000 fd:11f 37749829
的/ usr / lib64下/升
ibstdc ++。so.6.0.20
7ff3aa1f0000-7ff3aa1f2000 rw-p 000f8000 fd:11f 37749829
的/ usr / lib64下/升
ibstdc ++。so.6.0.20
7ff3aa1f2000-7ff3aa207000 rw-p 00000000 00:00 0
7ff3aa207000-7ff3aa228000 r-xp 00000000 fd:11f 37856921
/ usr / lib64 / l d-2.20.so 7ff3aa412000-7ff3aa417000 rw-p 00000000 00:00 0
7ff3aa424000-7ff3aa428000 rw-p 00000000 00:00 0
7ff3aa428000-7ff3aa429000 r - p 00021000 fd:11f 37856921
/ usr / lib64 / l d-2.20.so 7ff3aa429000-7ff3aa42a000 rw-p 00022000 fd:11f 37856921
/ usr / lib64 / l 7ff3aa429000-7ff3aa42a000 rw-p 00022000 fd:11f 37856921 / usr / lib64 / l
d-2.20.so
7ff3aa42a000-7ff3aa42b000 rw-p 00000000 00:00 0
7fff1dddb000-7fff1ddfc000 rw-p 00000000 00:00 0
[叠层]
7fff1ddfe000-7fff1de00000 r-xp 00000000 00:00 0
[VDSO]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0
[vsyscall]中止 (核心倾销)
答案 0 :(得分:11)
您有三个函数的返回类型不同于void
,而您没有有效的return
语句。
如果你提高警告级别,编译器会告诉你它们。使用g++ -Wall
时,我收到以下消息:
socc.cc: In member function ‘std::string Vehicle::setVehicleNo(std::string)’:
socc.cc:19:4: warning: no return statement in function returning non-void [-Wreturn-type]
}
^
socc.cc: In member function ‘std::string Vehicle::setColor(std::string)’:
socc.cc:29:4: warning: no return statement in function returning non-void [-Wreturn-type]
}
^
socc.cc: In member function ‘std::string Car::useSetColor(std::string)’:
socc.cc:60:7: warning: no return statement in function returning non-void [-Wreturn-type]
}
^
调用此类函数会导致未定义的行为。
来自C ++ 11标准:
6.6.3
return
声明2 ...
...
流出函数末尾相当于没有值的返回;这导致未定义 价值回归函数中的行为。
您可以通过添加合适的return
语句来解决问题。 E.g。
第一个功能:
string setVehicleNo(string num)
{
vehicleNo = num;
return vehicleNo; // Add this, maybe?
}
第二个功能:
string setColor(string someColor)
{
color = someColor;
return color; // Add this, maybe
}
第三个功能:
string useSetColor(string someColor)
{
return setColor(someColor); // Add the return, maybe?
}
答案 1 :(得分:2)
你的问题在这里:
string setVehicleNo(string num)
// ^^^^^^
{
vehicleNo = num;
}
您正在定义一个返回类型,但实际上并未返回值(您应该收到编译器警告)。
要修复,请指定void
返回类型,或返回值。
类似于您的其他功能。查看固定的demo here。
答案 2 :(得分:2)
在您的代码中执行以下更改:
首先 -
string setVehicleNo(string num)
// ^^^^^^
{
vehicleNo = num;
}
秒 -
string setColor(string someColor)
// ^^^^^^
{
color = someColor;
}
第三 -
string useSetColor(string someColor)
//^^^^^^
{
setColor(someColor);
}
您已将回复类型指定给string
但未返回任何值。将其更改为void
。查看此处所做的更改http://ideone.com/n1Wg15