Calvin Koder
Calvin Koder

Reputation: 35

LNK2019 error with functions used in main with Visual Studio 2012 C++

I receive the following error:

1>main.obj : error LNK2019: unresolved external symbol "void __cdecl setup(void)" (?setup@@YAXXZ) referenced in function _main 1>main.obj : error LNK2019: unresolved external symbol "void __cdecl display(void)" (?display@@YAXXZ) referenced in function _main 1>C:\code\Project1\Debug\Project1.exe : fatal error LNK1120: 2 unresolved externals

How do i resolve this?

Here is the main class, where the functions are used:

#include "interface.h"
using namespace std;

int main(){
    setup();
    display();
    system("pause");
    return 0;
}

Here is interface.cpp

#include <iostream>
#include <string>
#include "interface.h"
using namespace std;
class ui{
    void setup(){
        options[0][0]="Hello";
        options[1][0]="Hello";
        options[2][0]="Hello";
        options[3][0]="Hello";
        options[4][0]="Hello";
        options[5][0]="Hello";
        options[6][0]="Hello";
        options[7][0]="Hello";
        options[8][0]="Hello";
        options[9][0]="Hello";
    }
    void changeOption(int whatOption, string whatText,
                  string prop1, string prop2, string prop3, string prop4, string prop5){
        options[whatOption][0]=whatText;
        options[whatOption][1]=prop1;
        options[whatOption][2]=prop2;
        options[whatOption][3]=prop3;
        options[whatOption][4]=prop4;
        options[whatOption][5]=prop5;
    }
    void display(){
        for(int x=0;x<9;x++){
            cout<<options[x][0]<<endl;
        }
    }
};

and here is interface.h

#include <string>
//#include <iostream>
using namespace std;
#ifndef INTERFACE_H_INCLUDED
#define INTERFACE_H_INCLUDED
    void setup();
    extern string options[10][6];
    void changeOption(int whatOption, string whatText, string prop1, string prop2, string prop3, string prop4, string prop5);
    void display();
#endif INTERFACE_H

Upvotes: 1

Views: 2612

Answers (2)

mah
mah

Reputation: 39807

Your setup() function is inside the ui namespace but when you try to use it, you're not specifying that. Try ui::setup(); instead.

Same solution for display().

In addition to this, you need to reexamine your interface.h header file. Here, you're declaring these functions without enclosing the declaration in their namespace. This is why you were able to compile the main() function but not able to link it.

Upvotes: 0

Reed Copsey
Reed Copsey

Reputation: 564413

You declare these as global functions.

However, your implementation defines them inside of class ui{.

You can remove the class ui{ and matching closing parenthesis and it should work correctly.

Upvotes: 3

Related Questions