首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

简单C++代码,怎么调错呢

2012-09-23 
简单C++代码,如何调错呢?这个代码怎么错了呢?#include iostreamusing namespace stdint main(){int c

简单C++代码,如何调错呢?
这个代码怎么错了呢?

#include <iostream>

using namespace std;

int main(){
int c = 9;
int d = 9;
int r = add(c,d);
cout<<r;
return 0;
}

static int add(int a , int b){
return a+b;
}

[解决办法]
不论是使用 对象 或变量 或函数 或某一个自己定义的类型,在使用它们时,必须在使用的地方之前能够看到它们的定义或声明,否则会发生编译错误,错误指出你所使用的对象或变量或类型或函数没有声明

static int add(int a , int b){
return a+b;
}

把上面的这个静态函数写在调用它之前的位置,在调用它之前的位置或进行定义或进行声明
[解决办法]
使用函数前应该先声明,或者直接把定义放在使用前面 正解
[解决办法]

C/C++ code
static declaratorWhen modifying a variable, the static keyword specifies that the variable has static duration (it is allocated when the program begins and deallocated when the program ends) and initializes it to 0 unless another value is specified. When modifying a variable or function at file scope, the static keyword specifies that the variable or function has internal linkage (its name is not visible from outside the file in which it is declared). In C++, when modifying a data member in a class declaration, the static keyword specifies that one copy of the member is shared by all the instances of the class. When modifying a member function in a class declaration, the static keyword specifies that the function accesses only static members.当定义一个变量时,static关键字指明了这个变量具有静态作用域(在程序开始前就已分配内存,在程序结束时销毁)并且初始化为0或者指定一个值赋给它。当文件作用域定义一个static变量或者函数的时候,此关键字则指明此变量或者函数具有内部连接域(它的名字对外部文件(不在一个文件当中)不可见)在c++中,当在一个类当中声明一个static成员变量时,static关键字指明此成员变量只有一份副本为所有此类实例共用。当在类中指明一个static成员方法时,此关键字指明这个函数只能访问static成员变量
[解决办法]
http://blog.csdn.net/pony_maggie/article/details/5270514

热点排行