【发布时间】:2020-04-19 10:21:39
【问题描述】:
在我正在编写的程序中,我有 3 个文件。 main、函数和头文件。主文件使用头文件#include "implementation.cpp",函数文件使用头文件#include "driver.h"。在实现文件中已经声明了 3 个结构,每个结构都通过使用 extern 共享它们的值,在所需的最小重现代码中也可以看到。在头文件中,我声明了 3 个变量来使用这些外部变量。我相信这足以解决我的问题,但问题仍然存在。当我的代码尝试编译时,我收到以下错误消息。有很多,但我相信它们都源于同一个问题。那个问题是头文件能够找到类型客户、零件和制造商
In file included from implementation.cpp:8,
from driver.cpp:4:
driver.h:7:1: error: 'Customer' does not name a type
7 | Customer myCustomer;
| ^~~~~~~~
driver.h:8:1: error: 'Builder' does not name a type
8 | Builder myBuilder;
| ^~~~~~~
driver.h:9:1: error: 'Part' does not name a type
9 | Part myPart;
| ^~~~
driver.h:14:13: error: 'Part' was not declared in this scope
14 | std::vector<Part> readpartFile();
| ^~~~
driver.h:14:17: error: template argument 1 is invalid
14 | std::vector<Part> readpartFile();
| ^
driver.h:14:17: error: template argument 2 is invalid
driver.h:16:13: error: 'Customer' was not declared in this scope
16 | std::vector<Customer> readcustomerFile();
| ^~~~~~~~
driver.h:16:21: error: template argument 1 is invalid
16 | std::vector<Customer> readcustomerFile();
| ^
driver.h:16:21: error: template argument 2 is invalid
driver.h:18:13: error: 'Builder' was not declared in this scope
18 | std::vector<Builder> readbuilderFile();
| ^~~~~~~
driver.h:18:20: error: template argument 1 is invalid
18 | std::vector<Builder> readbuilderFile();
| ^
driver.h:18:20: error: template argument 2 is invalid
driver.h:20:24: error: 'Customer' does not name a type
20 | float complexity(const Customer& c, const std::vector<Part>& parts);
| ^~~~~~~~
driver.h:20:55: error: 'Part' was not declared in this scope
20 | float complexity(const Customer& c, const std::vector<Part>& parts);
| ^~~~
driver.h:20:59: error: template argument 1 is invalid
20 | float complexity(const Customer& c, const std::vector<Part>& parts);
| ^
driver.h:20:59: error: template argument 2 is invalid
driver.h:22:40: error: 'Part' was not declared in this scope
22 | void robotComplexity(const std::vector<Part>& vecB, const std::vector<Customer>& vecC);
| ^~~~
driver.h:22:44: error: template argument 1 is invalid
22 | void robotComplexity(const std::vector<Part>& vecB, const std::vector<Customer>& vecC);
| ^
driver.h:22:44: error: template argument 2 is invalid
driver.h:22:71: error: 'Customer' was not declared in this scope
22 | void robotComplexity(const std::vector<Part>& vecB, const std::vector<Customer>& vecC);
| ^~~~~~~~
driver.h:22:79: error: template argument 1 is invalid
22 | void robotComplexity(const std::vector<Part>& vecB, const std::vector<Customer>& vecC);
| ^
driver.h:22:79: error: template argument 2 is invalid
driver.h:24:38: error: 'Customer' was not declared in this scope
24 | double variability(const std::vector<Customer>& customerList, const std::vector<Builder>& builderList);
| ^~~~~~~~
driver.h:24:46: error: template argument 1 is invalid
24 | double variability(const std::vector<Customer>& customerList, const std::vector<Builder>& builderList);
| ^
driver.h:24:46: error: template argument 2 is invalid
driver.h:24:81: error: 'Builder' was not declared in this scope
24 | double variability(const std::vector<Customer>& customerList, const std::vector<Builder>& builderList);
| ^~~~~~~
driver.h:24:88: error: template argument 1 is invalid
24 | double variability(const std::vector<Customer>& customerList, const std::vector<Builder>& builderList);
| ^
driver.h:24:88: error: template argument 2 is invalid
driver.h:26:34: error: 'Builder' was not declared in this scope
26 | std::vector<double> buildAttempt(Builder b, double variaiblity, double complexityRobot);
| ^~~~~~~
driver.h:26:45: error: expected primary-expression before 'double'
26 | std::vector<double> buildAttempt(Builder b, double variaiblity, double complexityRobot);
| ^~~~~~
driver.h:26:65: error: expected primary-expression before 'double'
26 | std::vector<double> buildAttempt(Builder b, double variaiblity, double complexityRobot);
| ^~~~~~
In file included from driver.cpp:4:
implementation.cpp:43:19: error: ambiguating new declaration of 'std::vector<Part> readpartFile()'
43 | std::vector<Part> readpartFile() //function to read Builders, Customers and Parts text file
| ^~~~~~~~~~~~
In file included from implementation.cpp:8,
from driver.cpp:4:
driver.h:14:19: note: old declaration 'int readpartFile()'
14 | std::vector<Part> readpartFile();
| ^~~~~~~~~~~~
In file included from driver.cpp:4:
implementation.cpp:77:23: error: ambiguating new declaration of 'std::vector<Customer> readcustomerFile()'
77 | std::vector<Customer> readcustomerFile()
| ^~~~~~~~~~~~~~~~
In file included from implementation.cpp:8,
from driver.cpp:4:
driver.h:16:23: note: old declaration 'int readcustomerFile()'
16 | std::vector<Customer> readcustomerFile();
| ^~~~~~~~~~~~~~~~
In file included from driver.cpp:4:
implementation.cpp:100:22: error: ambiguating new declaration of 'std::vector<Builder> readbuilderFile()'
100 | std::vector<Builder> readbuilderFile()
| ^~~~~~~~~~~~~~~
In file included from implementation.cpp:8,
from driver.cpp:4:
driver.h:18:22: note: old declaration 'int readbuilderFile()'
18 | std::vector<Builder> readbuilderFile();
| ^~~~~~~~~~~~~~~
In file included from driver.cpp:4:
implementation.cpp:208:81: error: 'std::vector<double> buildAttempt(Builder, double, double)' redeclared as different kind of entity
208 | vector<double>buildAttempt(Builder b, double variaiblity, double complexityRobot) {
| ^
In file included from implementation.cpp:8,
from driver.cpp:4:
driver.h:26:21: note: previous declaration 'std::vector<double> buildAttempt'
26 | std::vector<double> buildAttempt(Builder b, double variaiblity, double complexityRobot);
| ^~~~~~~~~~~~
In file included from driver.cpp:4:
implementation.cpp: In function 'std::vector<double> buildAttempt(Builder, double, double)':
implementation.cpp:230:23: error: no matching function for call to 'complexity(Customer&, int&)'
230 | complexity(c,partsVec);
| ^
In file included from implementation.cpp:8,
from driver.cpp:4:
driver.h:20:7: note: candidate: 'float complexity(const int&, const int&)'
20 | float complexity(const Customer& c, const std::vector<Part>& parts);
| ^~~~~~~~~~
driver.h:20:34: note: no known conversion for argument 1 from 'Customer' to 'const int&'
20 | float complexity(const Customer& c, const std::vector<Part>& parts);
| ~~~~~~~~~~~~~~~~^
In file included from driver.cpp:4:
implementation.cpp:138:7: note: candidate: 'float complexity(const Customer&, const std::vector<Part>&)'
138 | float complexity(const Customer& c, const std::vector<Part>& parts)
| ^~~~~~~~~~
implementation.cpp:138:62: note: no known conversion for argument 2 from 'int' to 'const std::vector<Part>&'
138 | float complexity(const Customer& c, const std::vector<Part>& parts)
| ~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~
implementation.cpp:243:23: error: no matching function for call to 'complexity(Customer&, int&)'
243 | complexity(c,partsVec);
| ^
In file included from implementation.cpp:8,
from driver.cpp:4:
driver.h:20:7: note: candidate: 'float complexity(const int&, const int&)'
20 | float complexity(const Customer& c, const std::vector<Part>& parts);
| ^~~~~~~~~~
driver.h:20:34: note: no known conversion for argument 1 from 'Customer' to 'const int&'
20 | float complexity(const Customer& c, const std::vector<Part>& parts);
| ~~~~~~~~~~~~~~~~^
In file included from driver.cpp:4:
implementation.cpp:138:7: note: candidate: 'float complexity(const Customer&, const std::vector<Part>&)'
138 | float complexity(const Customer& c, const std::vector<Part>& parts)
| ^~~~~~~~~~
implementation.cpp:138:62: note: no known conversion for argument 2 from 'int' to 'const std::vector<Part>&'
138 | float complexity(const Customer& c, const std::vector<Part>& parts)
| ~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~
driver.cpp: In function 'int main()':
driver.cpp:24:27: error: no matching function for call to 'complexity(Customer&, int&)'
24 | complexity(c, partsVec);
| ^
In file included from implementation.cpp:8,
from driver.cpp:4:
driver.h:20:7: note: candidate: 'float complexity(const int&, const int&)'
20 | float complexity(const Customer& c, const std::vector<Part>& parts);
| ^~~~~~~~~~
driver.h:20:34: note: no known conversion for argument 1 from 'Customer' to 'const int&'
20 | float complexity(const Customer& c, const std::vector<Part>& parts);
| ~~~~~~~~~~~~~~~~^
In file included from driver.cpp:4:
implementation.cpp:138:7: note: candidate: 'float complexity(const Customer&, const std::vector<Part>&)'
138 | float complexity(const Customer& c, const std::vector<Part>& parts)
| ^~~~~~~~~~
implementation.cpp:138:62: note: no known conversion for argument 2 from 'int' to 'const std::vector<Part>&'
138 | float complexity(const Customer& c, const std::vector<Part>& parts)
| ~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~
driver.cpp:26:53: error: no matching function for call to 'complexity(Customer&, int&)'
26 | writeFile(buildAttempt(b, complexity(c, partsVec), variability(customerVec, builderVec)));
| ^
In file included from implementation.cpp:8,
from driver.cpp:4:
driver.h:20:7: note: candidate: 'float complexity(const int&, const int&)'
20 | float complexity(const Customer& c, const std::vector<Part>& parts);
| ^~~~~~~~~~
driver.h:20:34: note: no known conversion for argument 1 from 'Customer' to 'const int&'
20 | float complexity(const Customer& c, const std::vector<Part>& parts);
| ~~~~~~~~~~~~~~~~^
In file included from driver.cpp:4:
implementation.cpp:138:7: note: candidate: 'float complexity(const Customer&, const std::vector<Part>&)'
138 | float complexity(const Customer& c, const std::vector<Part>& parts)
| ^~~~~~~~~~
implementation.cpp:138:62: note: no known conversion for argument 2 from 'int' to 'const std::vector<Part>&'
138 | float complexity(const Customer& c, const std::vector<Part>& parts)
| ~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~
以下是重现此错误所需的最少代码量。为了方便起见,我只包含与零件相关的代码,因为客户和构建器的基本代码是相同的
我的主文件
#include <iostream>
#include <string>
#include "implementation.cpp"
#include <fstream>
#include <vector>
#include <random>
using namespace std;
int main()
{
Part p;
auto partsVec = readpartFile();return 0;
}
我的头文件
#include <vector>
#include <string>
#ifndef SOME_UNIQUE_NAME_HERE
#define SOME_UNIQUE_NAME_HERE
std::vector<Part> readpartFile();
#endif
从这一点注意到,程序不识别 Part myPart,因此将向量声明为类型 Part 不起作用。因此,会发生这些错误。
driver.h:9:1: error: 'Part' does not name a type
9 | Part myPart;
| ^~~~
driver.h:14:13: error: 'Part' was not declared in this scope
14 | std::vector<Part> readpartFile();
| ^~~~
最后是与Part相关的代码的实现文件
#include <iostream>
#include <string>
#include <sstream>
#include <fstream>
#include <algorithm>
#include "driver.h"
#include <random>
#include <vector>
#include <time.h>
using namespace std;
struct Part {
char partCode;
std::string partName;
int maximum;
int minimum;
int complexity;
};
extern Part myPart;
std::ifstream partsList("Parts.txt");
std::vector<Part> readpartFile() //function to read Builders, Customers and Parts text file
{
std::vector<Part> parts;
std::string line;
while (std::getline(partsList, line))
{
line.pop_back(); //removing '.' at end of line
std::string token;
std::istringstream ss(line);
Part part;
std::getline(ss, token, ':');
part.partCode = token[0];
std::getline(ss, part.partName, ':');
std::getline(ss, token, ':');
part.minimum = std::stoi(token);
std::getline(ss, token, ':');
part.maximum = std::stoi(token);
std::getline(ss, token, ':');
part.complexity = std::stoi(token);
parts.push_back(std::move(part));
}
return parts;
}
此代码应该足以为任何人提供找出我的问题并可能纠正问题的方法。谢谢你
P.S 应该注意的是,我已经尝试将我的结构放在我的头文件中,但这不起作用。
【问题讨论】:
-
不要
#include "implementation.cpp"。包含头文件,编译.cpp文件。与上一个问题一样,您的头文件也缺少包含保护。 -
这是对我的代码的修复还是对使问题更容易理解的评论?
-
这是关于您可能需要编写的所有 c++ 代码的注释。头文件包含类定义和函数声明等,
.cpp文件包含函数定义作为您的readpartFile()实现。 -
@TedLyngmo 是否包含守卫,请参阅我更新的关于我的头文件的问题。我已经使用警卫更新了它
-
你想要一个名为
myPart的全局变量吗?在大多数情况下,这是一个坏主意。此外,我根本看不到你使用它......
标签: c++ c++11 vector header-files