为什么我在下面的程序中得到宽度和高度变量的不同
#include <iostream>
using namespace std;
class my_space {
public :
void set_data(int width,int height) // taking the same name of the variable as the class member functions
{
width = width;
height = height;
}
int get_width()
{
return width;
}
int get_height()
{
return height;
}
private :
int width;
int height;
};
int main() {
my_space m;
m.set_data(4,5); // setting the name of private members of the class
cout<<m.get_width()<<endl;
cout<<m.get_height()<<endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
低于该程序的输出
sh-4.3$ main
1544825248 …Run Code Online (Sandbox Code Playgroud) 我试图找到下面代码的问题,需要知道链接器如何能够跟踪下面程序中的全局变量多重定义问题,我不太确定链接器内部如何执行检查,因此请求帮助澄清。
**hello.h**
#ifndef __HELLO_
#define __HELLO_
static int s = 20;
int g = 10;
#endif
Run Code Online (Sandbox Code Playgroud)
**cat hello1.c**
#include <stdio.h>
#include "hello.h"
//int g = 10;
int main()
{
g++;
s++;
printf("g : %d s : %d\n", g, s);
function();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
**cat hello2.c**
#include <stdio.h>
#include "hello.h"
//extern int g;
void function()
{
g++;
s++;
printf("g : %d s : %d\n", g, s);
}
Run Code Online (Sandbox Code Playgroud)
**gcc -save-temps hello1.c hello2.c -o hello**
hello2.o:(.data+0x4): multiple definition of `g'
hello1.o:(.data+0x4): first defined …Run Code Online (Sandbox Code Playgroud)