相关疑难解决方法(0)

内联变量如何工作?

在2016年奥卢ISO C++标准会议上,一项名为Inline Variables的提案被标准委员会投票选为C++ 17.

通俗地说,什么是内联变量,它们如何工作以及它们对什么有用?如何声明,定义和使用内联变量?

c++ c++17

106
推荐指数
3
解决办法
3万
查看次数

即使使用extern的c ++变量的多个定义

我一直在尝试修复我的多定义错误,大多数答案似乎是使用extern,但即使在这样做之后,该错误仍然存​​在

main.cpp

#include "header.h"

int main(){

ifstream indata;
indata.open(file.c_str());

int x;

while(indata){
    indata >> x;
    print(x);
}

return 0;
}
Run Code Online (Sandbox Code Playgroud)

functions.cpp

#include "header.h"

void print(int x){
    cout << x << endl;
}
Run Code Online (Sandbox Code Playgroud)

头文件

#ifndef _HEADER_H
#define _HEADER_H

#include <fstream>
#include <iostream>
#include <string>
#include <cstring>

using namespace std;

extern string file="testFile.txt";

void print(int x);

#endif
Run Code Online (Sandbox Code Playgroud)

生成文件

all: main

main.o: main.cpp header.h
    g++ -c main.cpp

functions.o: functions.cpp header.h
    g++ -c functions.cpp

main: main.o functions.o
    g++ main.o functions.o -o main

check: all …
Run Code Online (Sandbox Code Playgroud)

c++

2
推荐指数
2
解决办法
5819
查看次数

标签 统计

c++ ×2

c++17 ×1