C++ 尝试编写一个修改外部向量中值的方法

huangapple go评论57阅读模式
英文:

C++ Trying to write a method that changes a value in an extern vector

问题

以下是您要翻译的代码部分:

我有三个重要的文件:
main.cppcommon.h  EngineScript.cpp。我在 common 中声明了我的多维向量如下:
extern vector<vector<string>> PositionVector(vector<string>);

 main.cpp 中定义它:
vector<vector<string>> PositionVector(20, vector<string>(20, " ")); // 用字符串填充向量,本例中为一个空格

 EngineScript.cpp 中,我的方法如下:
class EngineObject : public EngineFunct {
public:
    int objectXcord = 0;
    int objectYcord = 0;
    string objectIcon = "?";

    void spawnObject() {
        PositionVector[objectXcord][objectYcord] = objectIcon;
    }
};

这段代码似乎存在一些问题,包括警告和错误。如果删除 spawnObject() 中的内容并将 PositionVector[obj.objectXcord][obj.objectYcord] = obj.objectIcon; 放入 main.cpp,它可以工作,但您想要的效果却无法实现。这个问题可能与指针和数组访问有关,但需要更多上下文信息才能确定问题的确切原因。希望这些信息对您有所帮助。

英文:

so I have three files here that are important:
main.cpp, common.h, EngineScript.cpp. I am declaring my multidimensional vector in common as extern vector&lt;vector&lt;string&gt;&gt; PositionVector(vector&lt;string&gt;);

I am defining it in main.cpp with

    vector&lt;vector&lt;string&gt;&gt; PositionVector(20,vector&lt;string&gt;(20,&quot; &quot;)); //populates the vector with a string, in this case, a space

my method looks like this in EngineScript.cpp

class EngineObject : public EngineFunct{
public:
int objectXcord = 0;
int objectYcord = 0;
string objectIcon = &quot;?&quot;;

void spawnObject(){
PositionVector[objectXcord][objectYcord] = objectIcon;
         }
     };

this does not work.
it throws these errors

In file included from /home/jake/vstests/C++/ncurses_projects/ncurses_2dengineproject/main.cpp:17:
/home/jake/vstests/C++/ncurses_projects/ncurses_2dengineproject/EngineScript.cpp: In member function ‘void EngineObject::spawnObject()’:
/home/jake/vstests/C++/ncurses_projects/ncurses_2dengineproject/EngineScript.cpp:50:31: warning: pointer to a function used in arithmetic [-Wpointer-arith]
   50 |     PositionVector[objectXcord][objectYcord] = objectIcon;
      |                               ^
/home/jake/vstests/C++/ncurses_projects/ncurses_2dengineproject/EngineScript.cpp:50:44: warning: pointer to a function used in arithmetic [-Wpointer-arith]
   50 |     PositionVector[objectXcord][objectYcord] = objectIcon;
      |                                            ^
/home/jake/vstests/C++/ncurses_projects/ncurses_2dengineproject/EngineScript.cpp:50:46: error: assignment of read-only location ‘*(PositionVector + (((sizetype)((EngineObject*)this)-&gt;EngineObject::objectXcord) + ((sizetype)((EngineObject*)this)-&gt;EngineObject::objectYcord)))’
   50 |     PositionVector[objectXcord][objectYcord] = objectIcon;
      |     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
make[2]: *** [CMakeFiles/Engine.dir/build.make:76: CMakeFiles/Engine.dir/main.cpp.o] Error 1
make[1]: *** [CMakeFiles/Makefile2:83: CMakeFiles/Engine.dir/all] Error 2
make: *** [Makefile:91: all] Error 2&gt; 

whats really grinding my gears right now is that if i get rid of the contents of spawnObject()
and put PositionVector[obj.objectXcord][obj.objectYcord] = obj.objectIcon; into main.cpp, it does work, it works fine with no errors or bugs and does almost what I wanted. Its late, I am not that great at C++ and I can't seem to find anything helpful on the interwebs. what simple mistake am I making? thanks in advance

edit 1
after making the changes recommended by john, it gets farther along but still gives errors, just these ones look more annoying

[ 25%] Building CXX object CMakeFiles/Engine.dir/main.cpp.o
[ 50%] Building CXX object CMakeFiles/Engine.dir/EngineScript.cpp.o
[ 75%] Linking CXX executable Engine
/usr/bin/ld: CMakeFiles/Engine.dir/main.cpp.o: warning: relocation against `_Z14PositionVectorB5cxx11&#39; in read-only section `.text._ZN12EngineObject11spawnObjectEv[_ZN12EngineObject11spawnObjectEv]&#39;
/usr/bin/ld: CMakeFiles/Engine.dir/main.cpp.o: in function `EngineObject::spawnObject()&#39;:
main.cpp:(.text._ZN12EngineObject11spawnObjectEv[_ZN12EngineObject11spawnObjectEv]+0x23): undefined reference to `PositionVector[abi:cxx11]&#39;
/usr/bin/ld: warning: creating DT_TEXTREL in a PIE
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/Engine.dir/build.make:129: Engine] Error 1
make[1]: *** [CMakeFiles/Makefile2:83: CMakeFiles/Engine.dir/all] Error 2
make: *** [Makefile:91: all] Error 2```


</details>


# 答案1
**得分**: 1

在common.h中:

```cpp
extern vector<vector<string>> PositionVector(vector<string>);

应该修改为:

extern vector<vector<string>> PositionVector;

你的版本声明了一个名为 PositionVector 的函数,这会让编译器感到困惑。

根据你得到的错误信息,似乎你没有在main.cpp中包含common.h。如果是这样的话,那么这是一个错误。你应该始终在声明全局变量的头文件中包含对应的全局定义的cpp文件。这可以让编译器有机会检查全局声明和全局定义是否一致。在上面的代码中,这是不成立的。

英文:

In common.h

extern vector&lt;vector&lt;string&gt;&gt; PositionVector(vector&lt;string&gt;);

should be

extern vector&lt;vector&lt;string&gt;&gt; PositionVector;

Your version declares a function called PositionVector which is confusing the compiler.

Judging by the errors you get it seems that you are not including common.h in main.cpp. If so then this is a mistake. You should always include the header file that makes a global declaration in the cpp file that makes the corresponding global definition. This gives the compiler a chance to check that the global declaration and global definition are consistent. Something that is not true in the code above.

huangapple
  • 本文由 发表于 2023年5月29日 14:43:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76355178.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定