【问题标题】:Fatal Error When Calling A Function From A Class Object从类对象调用函数时出现致命错误
【发布时间】:2019-10-22 22:27:09
【问题描述】:

我正在尝试启动一个必须从文件读取的程序,并在尝试调用对象上的函数时遇到致命错误代码。我只是确定这是错误的根源,因为我删除了 .cpp 文件中的所有代码,除了空函数体,它仍然产生相同的错误。最终,我试图用这段代码做的只是测试我是否可以从一个文件中读取两个单独的行,该文件包含两个整数,将它们拆分打印整数然后继续打印文件中不再是整数的每一行到控制台。抱歉,如果我的标题具有误导性,我不完全确定我在这里遇到了什么问题。

文件示例如下:

10 7  
6  2
##########          
#  ###   #
#  #     #
#        #
#        #
#        #
## #######         

这是我的 Ass3Main 文件代码:

#include <iostream>
#include <stdlib.h>
#include <fstream>
#include <istream>
#include <string>
#include "Maze.h"

using namespace std;

int main()
{
    Maze m;
    m.readFile("mazeSimple.txt");
    return 0;
}

这是我的 Maze.cpp 代码

//
// Created by Evan Walkoski on 10/22/2019.
//

#include "Maze.h"
#include <iostream>
#include <stdlib.h>
#include <fstream>
#include <istream>
#include <string>
#include <sstream>
#include <iterator>
#include <vector>

using namespace std;

void readFile(string filename)
{
    string line;
    ifstream myfile(filename);
    if (myfile.is_open())
    {
        getline(myfile, line);
        istringstream iss(line);
        vector<std::string> results((std::istream_iterator<std::string>(iss)),
                                    std::istream_iterator<std::string>());
        for (int i = 0; i < results.size(); i++)
        {
            cout << results[i] << endl;
        }
        cout << "~~~~~~~~~~~~~~" << endl;
        getline(myfile, line);
        istringstream iss2(line);
        for (int i = 0; i < 10; i++)
        {
            getline(myfile, line);
            cout << line << endl;
        }
        myfile.close();
    }
    else
        cout << "Unable to open file";
}

这是我的 Maze.h 代码

//
// Created by Evan Walkoski on 10/22/2019.
//

#ifndef ASS3_MAZE_H
#define ASS3_MAZE_H
#include <iostream>
#include <stdlib.h>
#include <fstream>
#include <istream>
#include <string>

using namespace std;

class Maze
{
    public:
        void readFile(string filename);
};


#endif //ASS3_MAZE_H

这是错误代码:

Scanning dependencies of target ass3
[ 33%] Building CXX object CMakeFiles/ass3.dir/Maze.cpp.obj
Maze.cpp
[ 66%] Linking CXX executable ass3.exe
LINK Pass 1: command "C:\PROGRA~2\MICROS~1\2019\COMMUN~1\VC\Tools\MSVC\1422~1.279\bin\Hostx86\x86\link.exe /nologo @CMakeFiles\ass3.dir\objects1.rsp /out:ass3.exe /implib:ass3.lib /pdb:C:\Users\Evan Walkoski\Documents\CSS\CSS 342\ass3\cmake-build-debug\ass3.pdb /version:0.0 /machine:X86 /debug /INCREMENTAL /subsystem:console kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib /MANIFEST /MANIFESTFILE:CMakeFiles\ass3.dir/intermediate.manifest CMakeFiles\ass3.dir/manifest.res" failed (exit code 1120) with the following output:
Ass3Main.cpp.obj : error LNK2019: unresolved external symbol "public: void __thiscall Maze::readFile(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (?readFile@Maze@@QAEXV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) referenced in function _main
ass3.exe : fatal error LNK1120: 1 unresolved externals
NMAKE : fatal error U1077: '"C:\Program Files\JetBrains\CLion 2019.2.2\bin\cmake\win\bin\cmake.exe"' : return code '0xffffffff'
Stop.
NMAKE : fatal error U1077: '"C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.22.27905\bin\HostX86\x86\nmake.exe"' : return code '0x2'
Stop.
NMAKE : fatal error U1077: '"C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.22.27905\bin\HostX86\x86\nmake.exe"' : return code '0x2'
Stop.
NMAKE : fatal error U1077: '"C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.22.27905\bin\HostX86\x86\nmake.exe"' : return code '0x2'
Stop.

【问题讨论】:

  • 旁注:使用&lt;cstdlib&gt; 而不是&lt;stdlib.h&gt;
  • void readFilevoid Maze::readFile 不同

标签: c++ function class object fatal-error


【解决方案1】:

对不起,UnholySheep 发表的评论

"void readFile 不等于 void Maze::readFile"

解决了我的问题。我开始使用来自 java 的 c++,所以我仍然做这样的愚蠢事情。将我的 .cpp 文件中的函数头更改为 void Maze::readFile 解决了问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-05-21
    • 2019-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多