C ++外部字符串数组不可见
c++ extern string array not visible
我是 C++ 开发的新手,当我在网上学习外部变量时 - 我尝试将其用于字符串变量及其工作 fine.But 我必须使用字符串变量,因为它看起来不像 working.Please如下
globals.h
#include <iostream>
using namespace std;
#ifndef SIMULATIONFILEPARSER_GLOBALS_H
#define SIMULATIONFILEPARSER_GLOBALS_H
//sequence of files to be execute
extern string ipFiles[];
#endif //SIMULATIONFILEPARSER_GLOBALS_H
globals.cpp
#include "../headers/globals.h"
//sequence of files to be execute
string ipFiles[] = {"in.relaxSubstrate", "in.relaxFluid"};
main.cpp
#include <iostream>
#include "Source/headers/globals.h"
int main() {
for (string &ipFileName :ipFiles) {
std::cout << ipFileName << std::endl;
}
return 0;
}
当我尝试运行这个项目时,它给出了以下错误
C:\Users\king\ClionProjects\SimulationFileParser\main.cpp: In function 'int main()':
C:\Users\king\ClionProjects\SimulationFileParser\main.cpp:5:30: error: range-based 'for' expression of type 'std::__cxx11::basic_string<char> []' has incomplete type
for (string &ipFileName :ipFiles) {
^
CMakeFiles\SimulationFileParser.dir\build.make:61: recipe for target 'CMakeFiles/SimulationFileParser.dir/main.cpp.obj' failed
mingw32-make.exe[3]: *** [CMakeFiles/SimulationFileParser.dir/main.cpp.obj] Error 1
mingw32-make.exe[3]: *** Waiting for unfinished jobs....
mingw32-make.exe[2]: *** [CMakeFiles/SimulationFileParser.dir/all] Error 2
CMakeFiles\Makefile2:66: recipe for target 'CMakeFiles/SimulationFileParser.dir/all' failed
mingw32-make.exe[1]: *** [CMakeFiles/SimulationFileParser.dir/rule] Error 2
CMakeFiles\Makefile2:78: recipe for target 'CMakeFiles/SimulationFileParser.dir/rule' failed
mingw32-make.exe: *** [SimulationFileParser] Error 2
Makefile:117: recipe for target 'SimulationFileParser' failed
这个循环不知道数组的大小。
for (string &ipFileName :ipFiles)
global.h
的变化
extern string ipFiles[2];
global.cpp
的变化
string ipFiles[2] = {"in.relaxSubstrate", "in.relaxFluid"};
在此之后您的代码应该可以正确编译。
编译器不会抱怨符号不可见。它告诉你,类型不完整:
range-based 'for' expression of type 'std::__cxx11::basic_string []' has incomplete type
在编译器知道数组的大小之前,它不能编译基于范围的for循环。要改变这一点,您需要声明一个完整的类型。这可能是一个具有明确大小的数组,或者 - 这是推荐的解决方案 - 标准容器1:
globals.h
#pragma once
#include <string>
#include <vector>
extern std::vector<std::string> ipFiles;
globals.cpp
std::vector<std::string> ipFiles{"in.relaxSubstrate", "in.relaxFluid"};
您不必更改 main.cpp。但是如果你想让它更花哨,你可以使用 auto
以及练习 const-correctness:
int main() {
for (const auto& ipFileName : ipFiles) {
std::cout << ipFileName << std::endl;
}
return 0;
}
1 编译时不需要知道标准容器的大小。编译器需要的只是受控序列 begin()
和 end()
的(前向)迭代器。另一方面,数组不提供这些作为成员函数,编译器需要生成它们。它需要知道生成等价于 end()
. 的大小
我是 C++ 开发的新手,当我在网上学习外部变量时 - 我尝试将其用于字符串变量及其工作 fine.But 我必须使用字符串变量,因为它看起来不像 working.Please如下
globals.h
#include <iostream>
using namespace std;
#ifndef SIMULATIONFILEPARSER_GLOBALS_H
#define SIMULATIONFILEPARSER_GLOBALS_H
//sequence of files to be execute
extern string ipFiles[];
#endif //SIMULATIONFILEPARSER_GLOBALS_H
globals.cpp
#include "../headers/globals.h"
//sequence of files to be execute
string ipFiles[] = {"in.relaxSubstrate", "in.relaxFluid"};
main.cpp
#include <iostream>
#include "Source/headers/globals.h"
int main() {
for (string &ipFileName :ipFiles) {
std::cout << ipFileName << std::endl;
}
return 0;
}
当我尝试运行这个项目时,它给出了以下错误
C:\Users\king\ClionProjects\SimulationFileParser\main.cpp: In function 'int main()':
C:\Users\king\ClionProjects\SimulationFileParser\main.cpp:5:30: error: range-based 'for' expression of type 'std::__cxx11::basic_string<char> []' has incomplete type
for (string &ipFileName :ipFiles) {
^
CMakeFiles\SimulationFileParser.dir\build.make:61: recipe for target 'CMakeFiles/SimulationFileParser.dir/main.cpp.obj' failed
mingw32-make.exe[3]: *** [CMakeFiles/SimulationFileParser.dir/main.cpp.obj] Error 1
mingw32-make.exe[3]: *** Waiting for unfinished jobs....
mingw32-make.exe[2]: *** [CMakeFiles/SimulationFileParser.dir/all] Error 2
CMakeFiles\Makefile2:66: recipe for target 'CMakeFiles/SimulationFileParser.dir/all' failed
mingw32-make.exe[1]: *** [CMakeFiles/SimulationFileParser.dir/rule] Error 2
CMakeFiles\Makefile2:78: recipe for target 'CMakeFiles/SimulationFileParser.dir/rule' failed
mingw32-make.exe: *** [SimulationFileParser] Error 2
Makefile:117: recipe for target 'SimulationFileParser' failed
这个循环不知道数组的大小。
for (string &ipFileName :ipFiles)
global.h
的变化extern string ipFiles[2];
global.cpp
的变化string ipFiles[2] = {"in.relaxSubstrate", "in.relaxFluid"};
在此之后您的代码应该可以正确编译。
编译器不会抱怨符号不可见。它告诉你,类型不完整:
range-based 'for' expression of type 'std::__cxx11::basic_string []' has incomplete type
在编译器知道数组的大小之前,它不能编译基于范围的for循环。要改变这一点,您需要声明一个完整的类型。这可能是一个具有明确大小的数组,或者 - 这是推荐的解决方案 - 标准容器1:
globals.h
#pragma once
#include <string>
#include <vector>
extern std::vector<std::string> ipFiles;
globals.cpp
std::vector<std::string> ipFiles{"in.relaxSubstrate", "in.relaxFluid"};
您不必更改 main.cpp。但是如果你想让它更花哨,你可以使用 auto
以及练习 const-correctness:
int main() {
for (const auto& ipFileName : ipFiles) {
std::cout << ipFileName << std::endl;
}
return 0;
}
1 编译时不需要知道标准容器的大小。编译器需要的只是受控序列
begin()
和 end()
的(前向)迭代器。另一方面,数组不提供这些作为成员函数,编译器需要生成它们。它需要知道生成等价于 end()
. 的大小