从类对象调用函数时发生致命错误

问题描述 投票:0回答:1

我试图开始必须从文件中读取的程序,并试图在对象上调用函数时出现致命错误代码。我只是确定这是错误的根源,因为我删除了.cpp文件中的所有代码(空函数体除外),并且仍然产生相同的错误。最终,我要使用此代码进行的所有操作都是测试是否可以从文件中读取两个单独的行,该文件包含两个int,将它们拆分后打印int,然后继续打印文件中不再为int的每一行控制台。抱歉,如果我的标题误导了我,我不确定在这里遇到什么问题。

文件示例将是这样:

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.
c++ function class object fatal-error
1个回答
0
投票

[抱歉,UnholySheep发表的评论

“ void readFile与void Maze :: readFile不同]

解决了我的问题。我开始使用来自Java的c ++,所以我仍然会做这样的愚蠢的事情。更改我的.cpp文件中的函数标头以使Maze :: readFile无效可以解决此问题。

© www.soinside.com 2019 - 2024. All rights reserved.