2017-09-24 727 views
-2

LNK2019解析的外部符号链接程序的失败与_main函数引用 “INT __cdecl invoke_main(无效)”(?invoke_main @@ YAHXZ)模板主要

我可以编译我的程序,但可不运行它。这是一个Windows控制台应用程序,它在Linker -> System -> SubSystem中设置。

#include "stdafx.h" 
#include <iostream> 
#include <queue> 
#include "puzzle.h" 
#include "state.h" 
#include <vector> 

using namespace std; 

template <typename Puzzle, typename State> 
int main() 
{ 
    Puzzle puzzle8; 
    State goalState = new State(); 
    State currentState = new State(); 

    //goal state 
    goalState.board = { { 1, 2, 3 }, 
         { 4, 5, 6 }, 
         { 7, 8, 0 } }; 
    //start state 
    currentState.board = { { 8, 2, 1 }, 
          { 5, 6, 0 }, 
          { 3, 7, 4 } }; 

    puzzle8.visited.push_back(currentState); //add to visited 

    while (!isGoalState(currentState, goalState)) 
    {   
     int f, best; 
     int board1Cost, board2Cost, board3Cost, board4Cost; 
     vector<State> newStates = expand(currentState); 
     int bestState = getLowestCost(newStates); 

     currentState = newStates.at(bestState).board; 
     cout << "New State found:" << endl; 
     printState(currentState); 
     puzzle8.visited.push_back(currentState); 

    } 
    return 0; 
} 

回答

1

您无法模板main。它需要是一个非常无聊,非常普通的功能。

目前尚不清楚为什么模板部分甚至在那里。我认为错误是有这条线,它应该被删除,因为StatePuzzle应该在它们各自的头文件中被定义,你正确的包含它们。

切记template函数实际上并不生成任何编译的代码,除非它们被使用,然后编译器将生成与所涉及的类型相关的代码。由于这个模板被声明并且从未被使用,它基本上被忽略了。

+0

你是对的。我删除它,它的工作。谢谢! –