• <bdo id='VALRY'></bdo><ul id='VALRY'></ul>

    1. <legend id='VALRY'><style id='VALRY'><dir id='VALRY'><q id='VALRY'></q></dir></style></legend>

        <tfoot id='VALRY'></tfoot>
        <i id='VALRY'><tr id='VALRY'><dt id='VALRY'><q id='VALRY'><span id='VALRY'><b id='VALRY'><form id='VALRY'><ins id='VALRY'></ins><ul id='VALRY'></ul><sub id='VALRY'></sub></form><legend id='VALRY'></legend><bdo id='VALRY'><pre id='VALRY'><center id='VALRY'></center></pre></bdo></b><th id='VALRY'></th></span></q></dt></tr></i><div id='VALRY'><tfoot id='VALRY'></tfoot><dl id='VALRY'><fieldset id='VALRY'></fieldset></dl></div>
      1. <small id='VALRY'></small><noframes id='VALRY'>

      2. C++ 错误:将‘char*’赋值给‘char [2] 时的类型不兼容

        C++ Error: Incompatible types in assignment of ‘char*’ to ‘char [2](C++ 错误:将‘char*’赋值给‘char [2] 时的类型不兼容)

            <legend id='TiWCf'><style id='TiWCf'><dir id='TiWCf'><q id='TiWCf'></q></dir></style></legend>

              <tbody id='TiWCf'></tbody>
              • <bdo id='TiWCf'></bdo><ul id='TiWCf'></ul>

                <small id='TiWCf'></small><noframes id='TiWCf'>

                <tfoot id='TiWCf'></tfoot>
                <i id='TiWCf'><tr id='TiWCf'><dt id='TiWCf'><q id='TiWCf'><span id='TiWCf'><b id='TiWCf'><form id='TiWCf'><ins id='TiWCf'></ins><ul id='TiWCf'></ul><sub id='TiWCf'></sub></form><legend id='TiWCf'></legend><bdo id='TiWCf'><pre id='TiWCf'><center id='TiWCf'></center></pre></bdo></b><th id='TiWCf'></th></span></q></dt></tr></i><div id='TiWCf'><tfoot id='TiWCf'></tfoot><dl id='TiWCf'><fieldset id='TiWCf'></fieldset></dl></div>

                  本文介绍了C++ 错误:将‘char*’赋值给‘char [2] 时的类型不兼容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我的构造函数有点问题.在我的头文件中,我声明:

                  I have a bit of a problem with my constructor. In my header file I declare:

                  char short_name_[2]; 
                  

                  • 和其他变量
                  • 在我的构造函数中:

                    Territory(std::string name, char short_name[2], Player* owner, char units);
                    void setShortName(char* short_name);
                    inline const char (&getShortName() const)[2] { return short_name_; }
                    

                    在我的 cpp 文件中:

                    In my cpp file:

                    Territory::Territory(std::string name, char short_name[2], Player* owner, 
                                         char units) : name_(name), short_name_(short_name), 
                                        owner_(owner), units_(units)
                    { }
                    

                    我的错误:

                    Territory.cpp:在构造函数‘Territory::Territory(std::string,char*, Player*, char)': Territory.cpp:15:33: 错误: 不兼容的类型将‘char*’赋值给‘char [2]’

                    Territory.cpp: In constructor ‘Territory::Territory(std::string, char*, Player*, char)’: Territory.cpp:15:33: error: incompatible types in assignment of ‘char*’ to ‘char [2]’

                    我已经发现 char[2] <=>char* 但我不知道如何处理这个关于我的构造函数和 get/setter 的问题.

                    I already figured out that char[2] <=> char* but I'm not sure how to handle this about my constructor and get/setters.

                    推荐答案

                    C++ 中的原始数组有点烦人,而且充满危险.这就是为什么除非你有很好的理由,否则你应该使用 std::vectorstd::array.

                    Raw arrays in C++ are kind of annoying and fraught with peril. This is why unless you have a very good reason to you should use std::vector or std::array.

                    首先,正如其他人所说,char[2]char* 不同,或者至少通常不同.char[2] 是一个大小为 2 的 char 数组,char* 是一个指向 char 的指针.他们经常感到困惑,因为数组会在需要时衰减为指向第一个元素的指针.所以这是有效的:

                    First off, as others have said, char[2] is not the same as char*, or at least not usually. char[2] is a size 2 array of char and char* is a pointer to a char. They often get confused because arrays will decay to a pointer to the first element whenever they need to. So this works:

                    char foo[2];
                    char* bar = foo;
                    

                    但反过来不行:

                    const char* bar = "hello";
                    const char foo[6] = bar; // ERROR
                    

                    更令人困惑的是,在声明函数参数时,char[] 等价于 char*.所以在你的构造函数中,参数 char short_name[2] 实际上是 char* short_name.

                    Adding to the confusion, when declaring function parameters, char[] is equivalent to char*. So in your constructor the parameter char short_name[2] is really char* short_name.

                    数组的另一个怪癖是它们不能像其他类型一样被复制(这是为什么函数参数中的数组被视为指针的一种解释).例如,我可以不能做这样的事情:

                    Another quirk of arrays is that they cannot be copied like other types (this is one explanation for why arrays in function parameters are treated as pointers). So for example I can not do something like this:

                    char foo[2] = {'a', 'b'};
                    char bar[2] = foo;
                    

                    相反,我必须遍历 foo 的元素并将它们复制到 bar 中,或者使用一些为我执行此操作的函数,例如 std::copy:

                    Instead I have to iterate over the elements of foo and copy them into bar, or use some function which does that for me such as std::copy:

                    char foo[2] = {'a', 'b'};
                    char bar[2];
                    // std::begin and std::end are only available in C++11
                    std::copy(std::begin(foo), std::end(foo), std::begin(bar));
                    

                    因此在您的构造函数中,您必须手动将 short_name 的元素复制到 short_name_ 中:

                    So in your constructor you have to manually copy the elements of short_name into short_name_:

                    Territory::Territory(std::string name, char* short_name, Player* owner, 
                                         char units) : name_(name), owner_(owner), units_(units)
                    { 
                        // Note that std::begin and std::end can *not* be used on pointers.
                        std::copy(short_name, short_name + 2, std::begin(short_name));
                    }
                    

                    正如你所看到的,这一切都很烦人,所以除非你有充分的理由,否则你应该使用 std::vector 而不是原始数组(或者在这种情况下可能是 std::string).

                    As you can see this is all very annoying, so unless you have a very good reason you just should use std::vector instead of raw arrays (or in this case probably std::string).

                    这篇关于C++ 错误:将‘char*’赋值给‘char [2] 时的类型不兼容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                    本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  What is inside .lib file of Static library, Statically linked dynamic library and dynamically linked dynamic library?(静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么?)
                  How do I load a C DLL from the SXS in Python?(如何从 Python 中的 SXS 加载 C DLL?)
                  Can Cython code be compiled to a dll so C++ application can call it?(Cython 代码可以编译成 dll 以便 C++ 应用程序可以调用它吗?)
                  Delay Loading DLLs(延迟加载 DLL)
                  Throwing C++ exceptions across DLL boundaries(跨 DLL 边界抛出 C++ 异常)
                  Loading a dll from a dll?(从 dll 加载 dll?)

                      <i id='FLZUD'><tr id='FLZUD'><dt id='FLZUD'><q id='FLZUD'><span id='FLZUD'><b id='FLZUD'><form id='FLZUD'><ins id='FLZUD'></ins><ul id='FLZUD'></ul><sub id='FLZUD'></sub></form><legend id='FLZUD'></legend><bdo id='FLZUD'><pre id='FLZUD'><center id='FLZUD'></center></pre></bdo></b><th id='FLZUD'></th></span></q></dt></tr></i><div id='FLZUD'><tfoot id='FLZUD'></tfoot><dl id='FLZUD'><fieldset id='FLZUD'></fieldset></dl></div>
                        <tbody id='FLZUD'></tbody>
                      <tfoot id='FLZUD'></tfoot>

                      <small id='FLZUD'></small><noframes id='FLZUD'>

                      • <legend id='FLZUD'><style id='FLZUD'><dir id='FLZUD'><q id='FLZUD'></q></dir></style></legend>
                        • <bdo id='FLZUD'></bdo><ul id='FLZUD'></ul>