C++ 未定义符号错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23124382/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C++ Undefined Symbol Error
提问by HarshVitra
I am Getting undefined symbol error for all the integer and char values.Please Help me.
The int x
y
and z
are not working and also the char value of function.
我收到所有整数和字符值的未定义符号错误。请帮助我。该int x
y
和z
不工作,也函数的char值。
#include <iostream.h>
#include <conio.h>
#include <math.h>
#include <string.h>
class Calculator
{
public:
int x;
int y;
int z;
char function;
void Calculate()
{
if(function=='+')
{z=x+y;}
else if(function=='-')
{z=x-y;}
else if(function=='*')
{z=x*y;}
else if(function=='/')
{z=x/y;}
else
{cout<<"Wrong Function!!!";}
}
};
void main()
{
clrscr();
Calculator working;
cout<<"Welcome!"<<endl;
cout<<"Enter your first number:"<<endl;
cin>>x;
cout<<"Enter your function:"<<endl;
cin>>function;
cout<<"Enter your second number:"<<endl;
cin>>y;
working.Calculate();
cout<<"Your Result is:"<<z<<endl;
getch();
}
回答by Pranit Kothari
Either use, std::cin
, std::cout
, std::endl
or include std namespace,
无论是使用std::cin
,std::cout
,std::endl
或有性病的命名空间,
using namespace std;
回答by niklasfi
This code compiles:
此代码编译:
#include <iostream>
#include <math.h>
#include <string.h>
using namespace std;
class Calculator
{
public:
int x,y;
float z;
void add()
{
cin>>x;
cin>>y;
z=x+y;
cout<<"The addition is:"<<z<<endl;
}
void substract()
{
cin>>x;
cin>>y;
z=x-y;
cout<<"The substraction is:"<<z<<endl;
}
void multiply()
{
cin>>x;
cin>>y;
z=x*y;
cout<<"The multiplication is:"<<z<<endl;
}
void divide()
{
cin>>x;
cin>>y;
z=x/y;
cout<<"The division is:"<<z<<endl;
}
};
int main()
{
cout<<"Hello User!"<<endl;
char Name[23];
cout<<"Enter your name:";
cin>>Name;
cout<<"Hy "<<Name<<endl;
cout<<"Calculator:"<<endl;
Calculator maths;
cout<<"Enter two numbers to Add:"<<endl;
maths.add();
cout<<"Enter two numbers to Substract:"<<endl;
maths.substract();
cout<<"Enter two numbers to Multiply:"<<endl;
maths.multiply();
cout<<"Enter two numbers to Divide:"<<endl;
maths.divide();
}
You are missing std::
qualifiers whenever you use cin
, cout
, or endl
. Either use std::cout
, std::cin
and std::endl
or include using namespace std;
in the beginning of your file.
std::
每次使用cin
, cout
, 或时都缺少限定符endl
。在文件的开头使用std::cout
,std::cin
和std::endl
或 include using namespace std;
。
回答by Chetan
You need to pass the values to the function Calculate. Variables x, y, z and function are not accessible outside the class and also u need a return type to the function so that you can get the output from the function Calculate.
您需要将值传递给函数Calculate。变量 x、y、z 和函数在类之外是不可访问的,并且您还需要函数的返回类型,以便您可以从函数 Calculate 中获取输出。
class Calculator
{
public:
int x;
int y;
int z;
char function;
int Calculate(int main_x,int main_y,char main_function)
{
x= main_x;
y=main_y;
function = main_function;
if(function=='+')
{z=x+y; return z;}
else if(function=='-')
{z=x-y; return z;}
else if(function=='*')
{z=x*y; return z;}
else if(function=='/')
{z=x/y; return z;}
else
{cout<<"Wrong Function!!!"; return 0;}
}
};
void main()
{
clrscr();
int main_x,main_y,main_z;
char main_function;
Calculator working;
cout<<"Welcome!"<<endl;
cout<<"Enter your first number:"<<endl;
cin>>x;
cout<<"Enter your function:"<<endl;
cin>>function;
cout<<"Enter your second number:"<<endl;
cin>>y;
main_z = working.Calculate(main_x,main_y,main_function);
cout<<"Your Result is:"<<main_z<<endl;
getch();
}
回答by Pankaj
Error is coming because you are trying to access class member variable x, y, z from outside in the main() where x, y, z is not declared.
错误即将到来,因为您试图在未声明 x、y、z 的 main() 中从外部访问类成员变量 x、y、z。
In order to calculate() work correctly x, y, z should get correct value, in your case these variable has garbage value.
为了使calculate() 正常工作,x、y、z 应该得到正确的值,在您的情况下,这些变量具有垃圾值。
回答by abhishek sharan
This is successful compiles version of your code.
这是您的代码的成功编译版本。
#include <iostream>
#include <cmath>
#include <cstring>
using namespace std;
class Calculator
{
public:
int x;
int y;
int z;
char function;
void Calculate()
{
if(function=='+')
{z=x+y;}
else if(function=='-')
{z=x-y;}
else if(function=='*')
{z=x*y;}
else if(function=='/')
{z=x/y;}
else
{cout<<"Wrong Function!!!"<<endl;}
}
};
int main()
{
Calculator working;
cout<<"Welcome!"<<endl;
cout<<"Enter your first number:"<<endl;
cin>>working.x;
cout<<"Enter your function:"<<endl;
cin>>working.function;
cout<<"Enter your second number:"<<endl;
cin>>working.y;
working.Calculate();
cout<<"Your Result is:"<<working.z<<endl;
return 0;
}