Меню

Ошибка c4716 должна возвращать значение

Permalink

Cannot retrieve contributors at this time

description title ms.date f1_keywords helpviewer_keywords ms.assetid

Learn more about: Compiler Warning (level 1) C4716

Compiler Warning (level 1) C4716

11/04/2016

C4716

C4716

d95ecfe5-870f-461f-a746-7913af98414b

‘function’ must return a value

The given function did not return a value.

Only functions with a return type of void can use the return command without an accompanying return value.

An undefined value will be returned when this function is called.

This warning is automatically promoted to an error. If you wish to modify this behavior, use #pragma warning.

The following sample generates C4716:

// C4716.cpp
// compile with: /c /W1
// C4716 expected
#pragma warning(default:4716)
int test() {
   // uncomment the following line to resolve
   // return 0;
}

ostream &operator<<(ostream &os, const PT &p) {
    os << "(" << p.x << "," << p.y << ")";

}

как исправить код?

Владимир Мартьянов's user avatar

задан 14 ноя 2017 в 12:31

user274179's user avatar

1

1 ответ

ostream& operator<<(ostream& os, const PT& p)
{
    os << "(" << p.x << "," << p.y << ")";
    return os;
}

ответ дан 14 ноя 2017 в 13:26

Jens's user avatar

JensJens

3,3652 золотых знака18 серебряных знаков43 бронзовых знака

  • Forum
  • Beginners
  • C4716 Error: Must return a value

C4716 Error: Must return a value

I am an absolute noob to programming and i’m having some serious problems. This is a problem i have for my programming class and I’m stumped. It’s saying a have to return a value to the main from a function but i just don’t understand why or why it’s won’t compile. PLEASE help! 🙁

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
//************************************
//* This program calculates the area 
//* of a rectangle.
//************************************
#include <iostream>
using namespace std;

double getLength();
double getWidth();
double getArea(double, double);
double displayData (double, double, double);

int main ()
{

	double rect_length,
           rect_width,
		   rect_area;
		   

	
	rect_length= getLength();
	rect_width= getWidth();
	
	rect_area=getArea(rect_length, rect_width);



return 0;
}

// ****************************************
//*THIS IS THE FUNCTION FOR GETTING THE 
//*RECTANGLE LENGTH
//*****************************************

double getLength(double)
{
	double length;

	cout << "Please enter the length of the rectangle: ";
	cin >> length;
	return length;

}

// ****************************************
//*THIS IS THE FUNCTION FOR GETTING THE 
//*RECTANGLE WIDTH
//*****************************************

double getWidth(double)
{
	double width;
	
	cout << "Please enter the width of the rectancle: ";
	cin >> width;
	return width;
}

// ****************************************
//* THIS FUNCTION TAKES THE RECTANGLE WIDTH
//* AND LENGTH AND MULTIPLIES THEM TO GIVE 
//* US THE RECTANGLES AREA.
//*****************************************

double getArea(double num1, double num2)
{ 
	return num1*num2;
	
}


// ****************************************
//* THIS FUNCTION ACCEPTS THE RECTANGLES 
//* LENGTH, WIDTH, AND AREA AND DIPLAYS 
//* THEM ON THE SCREEN. 
//*****************************************

double displayData(double rect_length, double rect_width, double rect_area)
{ 
	cout << "The length of the rectangle is " << rect_length << ". " << endl;
	cout << "The width of the rectangle is " << rect_width << ". " << endl;
	cout << "The area of the rectangle is " << rect_area << ". " << endl;
	
	
}

It saying «error C4716: ‘displayData’ : must return a value» when i try to compile it. What am i doing wrong??

Any help, or tips is greatly appreciated. Thank you!

Either declare the displayData Function as void like:

1
2
3
4
5
6
7
8
void displayData(double rect_length, double rect_width, double rect_area)
{ 
    cout << "The length of the rectangle is " << rect_length << ". " << endl;
    cout << "The width of the rectangle is " << rect_width << ". " << endl;
    cout << "The area of the rectangle is " << rect_area << ". " << endl;
    
    
}

or return a double.

1
2
3
4
5
6
7
8
double displayData(double rect_length, double rect_width, double rect_area)
{ 
    cout << "The length of the rectangle is " << rect_length << ". " << endl;
    cout << "The width of the rectangle is " << rect_width << ". " << endl;
    cout << "The area of the rectangle is " << rect_area << ". " << endl;
    
    return 0.0
}

Last edited on

Ok, tried both and neither compiled. I changed the function to void and the prototype to void

1
2
3
4
5
6
7
8
9

#include <iostream>
using namespace std;

double getLength();
double getWidth();
double getArea(double, double);
void displayData (double, double, double);
1
2
3
4
5
6
7
8
9
10


void displayData(double rect_length, double rect_width, double rect_area)
{ 
    cout << "The length of the rectangle is " << rect_length << ". " << endl;
    cout << "The width of the rectangle is " << rect_width << ". " << endl;
    cout << "The area of the rectangle is " << rect_area << ". " << endl;
    
    
}

and i changed it to return a double as well.

1
2
3
4
5
6
7
8
9

double displayData(double rect_length, double rect_width, double rect_area)
{ 
    cout << "The length of the rectangle is " << rect_length << ". " << endl;
    cout << "The width of the rectangle is " << rect_width << ". " << endl;
    cout << "The area of the rectangle is " << rect_area << ". " << endl;
    
    return 0.0
}

and the error for both of them was:

» 1>main.obj : error LNK2019: unresolved external symbol «double __cdecl getWidth(void)» (?getWidth@@YANXZ) referenced in function _main

1>main.obj : error LNK2019: unresolved external symbol «double __cdecl getLength(void)» (?getLength@@YANXZ) referenced in function _main

fatal error LNK1120: 2 unresolved externals»

I am clueless. I can’t figure out whats wrong.

Last edited on

Well, the functions called on main is getWidth() and getLength(). However, you’ve only defined the functions getLength(double) and getWidth(double). Remove those doubles and it should be fine.

Last edited on

Topic archived. No new replies allowed.

0 0 голоса
Рейтинг статьи
Подписаться
Уведомить о
guest

0 комментариев
Старые
Новые Популярные
Межтекстовые Отзывы
Посмотреть все комментарии

А вот еще интересные материалы:

  • Яшка сломя голову остановился исправьте ошибки
  • Ясность цели позволяет целеустремленно добиваться намеченного исправьте ошибки
  • Ясность цели позволяет целеустремленно добиваться намеченного где ошибка
  • Ошибка c2000 kyocera 2035
  • Ошибка c4703 используется потенциально неинициализированная локальная переменная указатель