简体   繁体   English

当我将C ++类拆分为标头和实现时,出现20个编译器错误,它们没有任何意义

[英]When I split up my C++ class into a header and implementation I get 20 compiler errors which don't make sense

I was splitting up my program into a header and implementation file per usual, however, when I tried to run the code, I got a ton of compile errors. 我通常将程序分为头文件和实现文件,但是,当我尝试运行代码时,遇到了大量编译错误。 This seems to be an issue with my computer or IDE, but I have not seen it before. 这似乎是我的计算机或IDE的问题,但我之前从未见过。 This should be relatively simple as it is for a class project. 就班级项目而言,这应该相对简单。 The code is as follows: 代码如下:

colorPicker.h colorPicker.h

#pragma once
class colorPicker {
private: 
    string colorArray[7];
public: 
    colorPicker(); 
    void printAllColors(); 
    string randomColor(); 
};

colorPicker.cpp colorPicker.cpp

#include "colorPicker.h"
#include "stdafx.h"
#include <iostream>
#include <string>
#include <ctime>
#include <stdio.h>
#include <stdlib.h>
using namespace std; 



colorPicker::colorPicker() {
    colorArray[0] = "Red"; 
    colorArray[1] = "Green";
    colorArray[2] = "Purple";
    colorArray[3] = "Yellow";
    colorArray[4] = "Orange";
    colorArray[5] = "Indigo";
    colorArray[6] = "Pink";
}

void colorPicker::printAllColors() {
    for (int i = 0; i < 7; i++) {
        cout << colorArray[i] << endl;
    }
}

string colorPicker::randomColor() {
    srand((unsigned)time(0)); 
    int j = 0; 
    j = rand() % 7; 
    return colorArray[j];
}

main.cpp main.cpp中

#include "colorPicker.h"
#include "stdafx.h"
#include <iostream>
#include <string>
#include <ctime>
#include <stdio.h>
#include <stdlib.h>
using namespace std;


int main() {
    colorPicker p;
    p.printAllColors(); 
    cout << "Random Color: " << p.randomColor() << endl; 
    system("pause");
    return 0; 
 }

There are 20 errors given by the compiler, however, they all seem to be stemming from two undeclared identifiers which are most definitely declared. 编译器给出了20个错误,但是它们似乎都来自于两个未声明的标识符,这些标识符是最明确声明的。 I am at a loss for what I could possibly do to fix it, and this project is due Sunday. 我不知所措,无法修复它,这个项目应在周日进行。 Thank you. 谢谢。

Here are the errors Tons of Errors 这是错误大量的错误

You need #include "colorPicker.h" in colorPicker.cpp . 您需要在colorPicker.cpp #include "colorPicker.h" Each .cpp file is handled basically independently by the compiler and they are all joined at the end by the "linker." 每个.cpp文件基本上由编译器独立处理,并且最后都由“链接器”连接在一起。 When the compiler looks at colorPicker.cpp without an include of the corresponding header, it's at a loss as to the definition of all the classes you're working with. 当编译器在不包含相应头的情况下查看colorPicker.cpp ,您正在使用的所有类的定义都将丢失。

There are a few things you are doing wrong. 您做错了几件事。 I'll just pick on a couple. 我只选一对。

Firstly, each header file you write should be self-contained - in the sense that, if it relies on content of some other headers, it includes that header. 首先,您编写的每个头文件都应该是独立的-从某种意义上来说,如果它依赖于其他头文件的内容,则它包含该头文件。 If a compilation unit (a formal name for a source file with a .cpp in your case) includes your header, it should not have to include something else your header depends on. 如果编译单元(在您的情况下为.cpp的源文件的正式名称)包含您的标头,则它不必包含标头所依赖的其他内容。

Second, it is a bad idea for a header to rely on any using directive, such as using namespace std . 其次,对于标头依赖任何using指令(例如using namespace std ,都是一个坏主意。 There are plenty of explanations of that available, so I won't repeat. 有很多可用的解释,所以我不再重复。

To understand the above, look at colorPicker.h 要了解以上内容,请查看colorPicker.h

 class colorPicker { private: string colorArray[7]; public: colorPicker(); void printAllColors(); string randomColor(); }; 

Firstly, this depends on string , but there is no definition of string visible in the header file. 首先,这取决于string ,但是在头文件中没有可见的string定义。 Usage of that type depends on the standard header <string> . 该类型的用法取决于标准头文件<string>

Second, that string type is within namespace std . 其次,该string类型在命名空间std So your header relies on the compilation unit (the source file that includes your header) having previously used a using directive ie using namespace std . 因此,您的标头依赖于以前使用过using指令(即using namespace std )的编译单元(包括标头的源文件)。

To fix these two problems, change the header to 要解决这两个问题,请将标题更改为

 #ifndef SOME_MACRO_UNIQUE_TO_YOUR_COLOR_PICKER_HEADER
 #define SOME_MACRO_UNIQUE_TO_YOUR_COLOR_PICKER_HEADER

 #include <string>

 class colorPicker
 {
     private: 
         std::string colorArray[7];
     public: 
       colorPicker(); 
       void printAllColors(); 
       std::string randomColor(); 
 };

 #endif

(I've also done some minor changes of layout, since I have various reasons to prefer that. (我还做了一些小的布局更改,因为出于各种原因我更喜欢这么做。

However, the #include <string> means that this version will not fail to compile, as yours does, if it is included by a compilation unit that does not have #include <string> . 但是, #include <string>意味着,如果该版本被没有#include <string>的编译单元所包含,则该版本将不会像您一样失败。

The usage of the fully qualified name std::string , rather than string , also means there is no dependence on the using directive using namespace std . 使用标准名称std::string而不是string ,也意味着不依赖于using namespace std的using指令。 It also means compilation errors can't be triggered in your header if your compilation unit has another using directive. 这也意味着如果您的编译单元具有另一个using指令,则无法在标头中触发编译错误。

I've also used an include guard, rather than #pragma once . 我还使用了include卫兵,而不是#pragma once Although most modern compilers support #pragma once , it is actually not standard C++ (a #pragma , by definition in the standard, is a compiler-specific hook). 尽管大多数现代编译器#pragma once支持#pragma once ,但实际上它不是标准的C ++(按照标准的定义, #pragma是编译器特定的钩子)。 Include guards are supported in standard C++. 标准C ++支持包含保护。

If you've done that, your code should mostly compile as is. 如果您这样做了,那么您的代码应该基本上可以按原样编译。 However, optionally, you may wish to 但是,可选地,您可能希望

  • remove the using directives using namespace std from your other files. 从其他文件中删除using namespace std的using指令。 If you do that, you will need to change the definition of colorPicker::randomColor() in colorPicker.cpp so it returns the fully qualified type std::string rather than string . 如果这样做,则需要在colorPicker::randomColor()更改colorPicker::randomColor()的定义,以便它返回标准类型std::string而不是string
  • Remove #include <string> from files that have #include "colorPicker.h" . 从具有#include "colorPicker.h"文件中删除#include <string> This is possible, since colorPicker.h now includes <string> . 这是可能的,因为colorPicker.h现在包括<string> This step is optional, since there is no problem with including standard headers more than once in a compilation unit. 此步骤是可选的,因为在编译单元中多次包含标准头文件没有问题。

A few other notes 其他一些注意事项

  • In C++, although it is not a major concern, it is usually considered better to use include <cstdio> and <cstdlib> rather than the C headers <stdio.h> and <stdlib.h> . 在C ++中,尽管不是主要问题,但通常认为使用include <cstdio><cstdlib>比使用C头文件<stdio.h><stdlib.h>更好。
  • Your code is calling srand((unsigned)time(0)) whenever colorPicker::randomColor() is called. 每当调用colorPicker::randomColor()时,您的代码就会调用srand((unsigned)time(0)) It is better to only call it once in an entire program, not in a function that may be called multiple times. 最好只在整个程序中调用一次,而不是在可能多次调用的函数中调用。

You may need add head file and in colorPicker.h. 您可能需要在colorPicker.h中添加头文件。 And the std namespace is needed while using string. 使用字符串时需要std名称空间。

BTW, the header guards is recommended strongly. 顺便说一句,强烈建议使用头部保护装置。

#ifndef COLOR_PICKER_H
#define COLOR_PICKER_H

#pragma once

#include <string>

class colorPicker {
private:
    std::string colorArray[7];
public:
    colorPicker();
    void printAllColors();
    std::string randomColor();
};

#endif

A header file should be self-contained as far as #include s go. 头文件应该是自包含的,直到#include为止。 That means that you should be able to #include the header file without having to include other stuff before it! 这意味着您应该能够#include头文件, 不必在其之前包含其他内容!

Your colorPicker.h does not meet that requirement. 您的colorPicker.h不满足该要求。 It apparently uses std::string from the standard library but does not have an #include <string> on top, so everyone who uses colorPicker.h has to remember to put an #include <string> before it. 它显然使用了标准库中的std::string ,但顶部没有#include <string> ,因此使用colorPicker.h每个人都必须记住在其前面加上#include <string> That's pretty annoying. 真烦人。

Even worse, colorPicker.h refers to std::string as string , which implies a using std::string; 更糟糕的是, colorPicker.hstd::string称为string ,这意味着using std::string; or using namespace std; using namespace std; somewhere before any #include "colorPicker.h" line, and both of those are very bad coding style in C++, if not used in tighter scopes. 任何#include "colorPicker.h"行之前的某处,如果未在更严格的范围内使用,则这两种都是C ++中非常糟糕的编码样式。

Here's how to fix the header file: 这是修复头文件的方法:

#pragma once
#include <string>

class colorPicker {
private: 
    std::string colorArray[7];
public: 
    colorPicker(); 
    void printAllColors(); 
    std::string randomColor(); 
};

As far as your *.cpp files go, I can see that you are using #include "stdafx.h" . 至于您的* .cpp文件,我可以看到您正在使用#include "stdafx.h" Why? 为什么? It's a non-standard Microsoft thing completely unnecessary in your case. 对于您而言,这是非标准的Microsoft事情,完全没有必要。 You are also using it incorrectly. 您也使用不正确。 It must be the first include. 它必须是第一个包含。 Just remove it entirely. 只需将其完全删除即可。

Some other suggested cleanup: 建议进行其他一些清理:

  • using namespace std; lines in *.cpp files is not as bad as in header files, but if I were you, I'd just get rid of it completely. * .cpp文件中的行并不比头文件中的行糟,但是如果我是你,我会完全摆脱它。 Just use complete names. 只需使用完整名称即可。 Say std::cout , not cout . std::cout ,而不是cout And so on. 等等。 It's just the most consistent way and it avoids a lot of trouble. 这只是最一致的方法,它避免了很多麻烦。
  • You include a lot of headers which you don't need. 您包括许多不需要的标题。 For example, what's <ctime> for? 例如, <ctime>是做什么的?
  • Don't use system("pause"); 不要使用system("pause"); . Do not look for artificial ways of pausing a command-line program. 不要寻找人为的方式来暂停命令行程序。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 C++ 中,我遇到了一个我无法理解的编译器错误 - In C++, I'm getting a compiler error that I can't make sense of 尝试设置GNU C ++编译器,但是在尝试编译hello.cpp时出现一个我不明白的错误 - Trying to set up the GNU C++ COMPILER, but I get an error I don't understand when I try to compile hello.cpp 我不明白我的编译器如何在C ++中获得此输出 - I don't understand how my compiler got this output in C++ 为什么在使用重载赋值运算符时会出错,但我没有使用编译器提供的赋值运算符? - Why do I get errors when I use the overloaded assignment operator, but I don't get using the compiler-supplied one? 我不明白编译器错误 - I don't understand the compiler errors VS,C++。 代码编辑器中没有错误但是在调试时,Output Window 有很多没有意义的错误 - VS,C++. No errors in the Code Editor however when debugging, the Output Window has lots of errors that don't make sense C++ 编译器,我可以将其与我的应用程序捆绑在一起 - C++ compiler which I can bundle with my application 尝试在C ++中包含头文件时为什么会出现错误? - Why do I get errors when I try to include a header file in C++? 我是 C++ 的新手,我得到了这个错误帮助。 我很确定这是一个简单的解决方法,但我查找的答案没有意义 - I'm new to C++ and I got this error help. I'm pretty sure its an easy fix but the answers i looked up didn't make sense 我正在从C转向C ++,我没有得到这个类的创建 - I'm moving from C to C++ and I don't get this creation of a class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM