未解析的外部定义构造函数?

时间:2016-09-11 05:05:22

标签: c++ class linker

我不能为我的生活弄清楚为什么没有看到实现函数,并且没有解析对构造函数的调用。任何想法都将不胜感激。

错误:

1>Debug\MeGLWindow.obj : warning LNK4042: object specified more than once; extras ignored
1>MeApp.obj : error LNK2019: unresolved external symbol "public: __thiscall MeGLWindow::MeGLWindow(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (??0MeGLWindow@@QAE@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) referenced in function "public: __thiscall MeApp::MeApp(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (??0MeApp@@QAE@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z)
1>C:\Users\FrizzleFry\documents\visual studio 2012\Projects\ConsoleApplication1\Debug\ConsoleApplication1.exe : fatal error LNK1120: 1 unresolved externals

App Header

#ifndef ME_APP
#define ME_APP

#include "MeGLWindow.h"
#include <string>

class MeApp {

private:
    MeGLWindow* meWind;
    std::string app;

public:

    MeApp() {}
    MeApp(std::string);
    ~MeApp();
    void run();

};


#endif

实施

#include "MeApp.h"

#include <SDL.h>
#include <gl\glew.h>
#include <SDL_opengl.h>
#include <gl\glu.h>
#include <string>

MeApp::MeApp(std::string appName) {

    app = appName;
    meWind = new MeGLWindow(app);


}


MeApp::~MeApp() {

    //delete[] meWind;
    //meWind = NULL;

}

void MeApp::run() {

    bool quit = false;

    //Event handler
    SDL_Event e;

    //Enable text input
    SDL_StartTextInput();

    //While application is running
    while( !quit )
    {
        //Handle events on queue
        while( SDL_PollEvent( &e ) != 0 )
        {
            //User requests quit
            if( e.type == SDL_QUIT )
            {
                quit = true;
            }
            //Handle keypress with current mouse position
            else if( e.type == SDL_TEXTINPUT )
            {
                int x = 0, y = 0;
                SDL_GetMouseState( &x, &y );
                //handleKeys( e.text.text[ 0 ], x, y );
            }


            //meWind->show();
        }

        //Disable text input
        SDL_StopTextInput();
    }
}

窗口标题和实现:

#ifndef ME_GL_WINDOW
#define ME_GL_WINDOW

#include "SDL.h"
#include "GL\glew.h"
#include "SDL_opengl.h"
#include "GL\glu.h"
#include <string>

class MeGLWindow {

private:
    std::string appName;

    int SCREEN_WIDTH, SCREEN_HEIGHT;
    SDL_Window* meWind;
    SDL_GLContext meContext;


public:

    MeGLWindow() {}
    MeGLWindow(std::string);
    ~MeGLWindow();
    void show();

};


#endif

CPP

#include <SDL.h>
#include <gl\glew.h>
#include <SDL_opengl.h>
#include <gl\glu.h>
#include <string>

#include "MeGLWindow.h"
#include <iostream>
#include <string>



MeGLWindow::MeGLWindow(std::string app) {

    appName = app;
    if(SDL_Init( SDL_INIT_VIDEO) < 0) {
        std::cout << "Failed to initilialize SDL!" << std::endl;
        exit(1);
    }

    SCREEN_HEIGHT = 640;
    SCREEN_WIDTH  = 480;

    //Use OpenGL 3.1 core
    SDL_GL_SetAttribute( SDL_GL_CONTEXT_MAJOR_VERSION, 3 );
    SDL_GL_SetAttribute( SDL_GL_CONTEXT_MINOR_VERSION, 1 );
    SDL_GL_SetAttribute( SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE );

    //Create window
    meWind = SDL_CreateWindow( appName.c_str() , SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN );
    if( meWind == NULL )
    {
        std::cout << "Failed to create window!" << std::endl;
        exit(2);
    }

    meContext = SDL_GL_CreateContext( meWind );
    if( meContext = NULL ) {
        std::cout << "Failed to create GL Context!" << std::endl;
        exit(3);
    }

    glewExperimental = GL_TRUE; 
    GLenum glewerror = glewInit();
    if( glewerror != GLEW_OK )
    {
        std::cout << "Error initializing GLEW!" << std::endl;
        exit(4);
    }

    //use vsync
    if( SDL_GL_SetSwapInterval( 1 ) < 0 )
    {
        std::cout << "Unable to set vsync!" << std::endl;
        //printf( "warning: unable to set vsync! sdl error: %s\n", sdl_geterror() );
    }

    //initialize opengl
    //( !initgl() )



}

MeGLWindow::~MeGLWindow() {
    //glDeleteProgram( gProgramID );

    //Destroy window    
    SDL_DestroyWindow( meWind );
    meWind = NULL;

    //Quit SDL subsystems
    SDL_Quit();
}

void MeGLWindow::show() {

    glClear( GL_COLOR_BUFFER_BIT );

    SDL_GL_SwapWindow( meWind );

}

调用文件

#include <SDL.h>
#include <gl\glew.h>
#include <SDL_opengl.h>
#include <gl\glu.h>
#include <stdio.h>
#include <string>

#include "MeApp.h"

int main( int argc, char* args[] )
{
    MeApp app("My Application");

    app.run();

    return 0;
}

修改

好吧,我想也许.obj文件已损坏,并尝试删除它并重建。他们被重建但仍然给了我这个错误。

如果我引入非敏感代码(我在MeGLWindow.h中添加了行a作为类成员)并且构建由于以下原因而失败:

1>c:\users\frizzlefry\documents\visual studio 2012\projects\consoleapplication1\consoleapplication1\meglwindow.h(22): error C2146: syntax error : missing ';' before identifier 'MeGLWindow'
1>c:\users\frizzlefry\documents\visual studio 2012\projects\consoleapplication1\consoleapplication1\meglwindow.h(22): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>
1>Build FAILED.

然后我去了项目目录并删除了整个调试文件夹,保存了两个.dll文件。

然后我删除了无意义的代码并重建了解决方案无济于事。我收到了同样的unresolved external symbol错误。

当我最初创建文件时,我意外地将文件命名为meGLWindow.h,并且在重命名文件后我开始收到此错误。它不会让我只用资本重命名文件,它会说文件已经存在。所以我将它重命名为不同的东西(2MeGLWindow.h),然后删除项目文件夹中的原始文件并将其重命名为MeGLWindow.h

然后发生了这个错误(但我添加了代码并且认为这不是原因),特别是因为如果我删除了MeGLWindow函数调用(如new MeGLWindow(app);),则不会发生构建错误。只有当我尝试使用main函数或app类创建MeGLWindow的实例时。

我正在考虑将面食复制到新的解决方案中。但也许解决方案中有某种源文件列表指向我最初创建的错误的meGLWindow.h文件?

EDIT2

刚吃饱了,这是5个档案。我做了一个新项目,复制粘贴标题和cpp文件,它就像一个魅力。我永远不会再简单地重命名头文件。

1 个答案:

答案 0 :(得分:1)

警告LNK4042有点欺骗性,实际上这意味着对象FILE被指定了一次,所以出于某种原因你已经有MeGLWindow.obj,并且它不包含MeGLWindow(std::string)的符号,因此错误。可能的原因是:

  • 构建无法从尚未实现该构造函数的时间删除旧的MeGLWindow.obj
  • Build甚至不会尝试删除旧的MeGLWindow.obj
  • 在正确的版本之前,其他一些MeGLWindow.obj会弹出。

解决方案:尝试清理构建/重建/手动删除Debug/MeGLWindow.obj;仔细检查构建选项,输出文件名。检查安全性/ UAC /访问权限。希望这会有所帮助。