C++ 错误:尝试访问全局变量时变量未命名类型

Posted

技术标签:

【中文标题】C++ 错误:尝试访问全局变量时变量未命名类型【英文标题】:C++ error: Variable does not name a type when attemtping to access global variables 【发布时间】: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 的全局变量吗?在大多数情况下,这是一个坏主意。此外,我根本看不到你使用它...... 【参考方案1】:

你的头文件应该是这样的

#ifndef SOME_UNIQUE_NAME_HERE
#define SOME_UNIQUE_NAME_HERE

#include <vector>
#include <string>

struct Part 
    char partCode;
    std::string partName;
    int maximum;
    int minimum;
    int complexity;

;
extern Part myPart;

std::vector<Part> readpartFile();

#endif

你的主文件应该是这样的

#include <iostream>
#include <string>
#include <fstream>
#include <vector> 
#include <random>
#include "myheaderfile.h" // sorry I don't know what your header file is really called
using namespace std;

int main()

    Part p;
    auto partsVec =  readpartFile();return 0;

你的实现文件应该是这样的

#include <iostream>
#include <string>
#include <sstream> 
#include <fstream>
#include <algorithm> 
#include "myheaderfile.h" // sorry I don't know what your header file is really called
#include <random>
#include <vector>
#include <time.h>
using namespace std;

std::vector<Part> readpartFile() //function to read Builders, Customers and Parts text file

    std::ifstream partsList("Parts.txt");

    ...

【讨论】:

【参考方案2】:

重组。

驱动程序.h

#ifndef DRIVER_H
#define DRIVER_H

#include <string>
#include <vector>

struct Part                       // class definition
    char partCode;
    std::string partName;
    int maximum;
    int minimum;
    int complexity;
;

std::vector<Part> readpartFile();  // (extern) forward declaration of a function

#endif

implementation.cpp

// only include what you use:

#include "driver.h"

//#include <algorithm>  // why?
#include <fstream>
//#include <iostream>   // why?
//#include <random>     // why?
#include <sstream>
#include <string>
#include <vector>
//#include <time.h>     // why?

// using namespace std; // don't

std::vector<Part>
readpartFile() // function to read Builders, Customers and Parts text file

    std::ifstream partsList("Parts.txt"); // don't make this global
    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;

main.cpp

#include "driver.h" // not implementation.cpp

// unused header files removed

int main() 
    Part p;
    auto partsVec = readpartFile();

编译 - 没有头文件,只有 .cpp 文件:

g++ -Wall -Wextra -pedantic -pedantic-errors main.cpp implementation.cpp -o program

【讨论】:

【参考方案3】:

您对 cpp 文件中的内容和头文件中的内容似乎有一些误解。

但首先 - 不要包含 cpp 文件!这是你应该包含的头文件。

也是这样

#include "implementation.h"  // instead of implementation.cpp

implementation.h 必须具有 Part 结构的定义。所以把它从cpp文件移到头文件。喜欢:

implementation.h

#ifndef SOME_UNIQUE_NAME_HERE
#define SOME_UNIQUE_NAME_HERE

#include <vector>
#include <string>

struct Part 
    char partCode;
    std::string partName;
    int maximum;
    int minimum;
    int complexity;
;


std::vector<Part> readpartFile();

#endif

然后将头文件包含在 implementation.cpp 中

implementation.cpp

#include <iostream>
#include <string>
#include <sstream> 
#include <fstream>
#include <algorithm> 
#include "driver.h"
#include <random>
#include <vector>
#include <time.h>

#include "implementation.h"  // Notice

using namespace std;


std::vector<Part> readpartFile() 

    ...

同样你将它包含在需要使用结构Part的其他文件中

【讨论】:

整个故事的开始是因为 OP 在头文件中定义了一些全局变量(参见上面的myPart)。也许您可以扩展此答案以显示声明和定义该全局变量的正确方法。 @john 好吧,OP 在评论中说 myPart 将被删除 - 而 OP 现在已经这样做了。所以如果我写myPart,我想我的回答会很奇怪 @john 还有,现在有 3 个答案 - 都有很好的信息 - 所以 OP 现在应该能够解决问题, 您也许是对的,但在之前的问题中,他说全局变量对于程序的运行至关重要。有关驱动程序代码的某些内容需要它们。哦,好吧。

以上是关于C++ 错误:尝试访问全局变量时变量未命名类型的主要内容,如果未能解决你的问题,请参考以下文章

C++ Cmath.h 问题导致变量不在全局命名空间中

使用 C++ 访问 Lua 全局表

c++ int类型默认值是多少?

静态/全局变量问题

如何访问 C 中的阴影全局变量?

从匿名函数汇编脚本访问全局变量