找不到标识符-Noob C ++问题 [英] Identifier not found - Noob C++ question

查看:103
本文介绍了找不到标识符-Noob C ++问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始学习C ++,我来自C#.下面是一些我试图计算重载方法的代码,但出现错误找不到标识符"?请保持温柔:

I have just started learning C++, I come from a C# background. Below is some code where I am trying to cal an overloaded method, but I get the error "identifier not found"? Please be gentle:

#include "stdafx.h"
#include <iostream>

using namespace std;

int _tmain()
{
	char input = ''0'';
	while(input != ''d'' && input != ''g'')
	{
		cout << "Please select a company to recieve a quote from\nDHL - (d)\nGlobalMail - (g)\n\n(select either g or d)->";
		cin >> input;
	}

	cout << "\n\n";
	
	switch (input)
	{
	case ''d'':
		
         break;

	case ''g'':
		cout << "GlobalMail, great choice! Please enter the weight of your parcel\n->";
		int weight = NULL;
		cin >> weight;
		weight = calcPostage(weight); //this line breaks
		cout << "Your package will cost R" << weight;
		break;
	}

	return 0;
}

int calcPostage(int weight)
{
	return weight * 108;
}

int calcPostage(int length, int width, int height)
{
	return (length * width * height) / 5000;
}</iostream>

推荐答案

您需要在main之前提到重载calcPostage函数的函数原型,这将解决问题.
You need to mention the function prototype for overloaded calcPostage functions before main,that will solve the issue.


编译器从源文件的顶部到底部开始工作.

函数原型是对将要存在的函数的定义,因此编译器知道不会使代码编译失败.

如果发现尚未定义的功能,它将失败,以同样的方式,如果定义了两次,则将失败.

正如Harish所说的那样:

int calcPostage(int);
int calcPostage(int,int,int);

在main将要修复代码之前,以便编译器知道这些功能将在代码的将来存在.
The compiler works from the top of a source file to the bottom.

A function prototype is a definition of the function that will exist and so the compiler knows not to fail the code compile.

If it finds a function that is not defined already it will fail, in the same way it will fail if a function is defined twice.

As Harish has said putting:

int calcPostage(int);
int calcPostage(int,int,int);

before the main will fix the code so that the compiler knows that these function will exist at some point in the future in the code.


这篇关于找不到标识符-Noob C ++问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆