Friday, 30 June 2017

unresolved externals Error in C++ Project



I wrote my project using a simple editor and I compile them by using Microsoft vc++ compiler through command line interface, I am getting the following error:




/out:Main.exe Main.obj



Main.obj : error LNK2019: unresolved
external symbol "public: void
__thiscall Ac count::debit(int)" (?debit@Account@@QAEXH@Z) referenced
in function _main



Main.obj : error LNK2019: unresolved

external symbol "public: int
__thiscall Acc ount::getBalance(void)" (?getBalance@Account@@QAEHXZ)
referenced in function _main



Main.obj : error LNK2019: unresolved
external symbol "public: __thiscall
Account ::Account(int)"
(??0Account@@QAE@H@Z) referenced in
function _main Main.exe : fatal error
LNK1120: 3 unresolved externals





Here is the code:



//File : Account.h
class Account{
public:
Account( int );
void credit( int );
void debit( int );

int getBalance();
private:
int balance;
};




//File:Account.cpp


#include
using std::cout;
using std::endl;

#include "Account.h"

Account::Account( int initialbalance ){
balance = 0;

if( initialbalance > 0 )

balance = initialbalance;

if ( initialbalance < 0 )
cout<<"Initial Balance is empty\n"<}

void Account::credit( int amount ){
balance = balance + amount;
}


void Account::debit( int amount ){
if( amount <= balance )
balance = balance - amount;
else
cout<<"Debit amount exceed balance amount\n"<}

int Account::getBalance(){
return balance;
}





//File : Main.cpp
#include
using std::cout;
using std::endl;
using std::cin;


#include "Account.h"

int main(){
Account obj(50);

cout<<"Account balance Rs. "< int withdraw;

cout<<"Withdrawal amount for your account\n"< cin>>withdraw;


cout<<"Withdrawing ....."< obj.debit( withdraw );

cout<<"Final account balance : "<

return 0;
}



I have first compiled Account.cpp by using "cl /LD Account.cpp" , then when i try to compile "Main.cpp" I get these error , to be specific I want to know how to use a compiled .dll or .obj file in my client code that uses these compiled files when their source code is not available .



Thanks in advance.


Answer



Apparently you have forgotten to include the Account.obj in your command line arguments: /out:Main.exe Main.obj Account.obj


No comments:

Post a Comment

casting - Why wasn&#39;t Tobey Maguire in The Amazing Spider-Man? - Movies &amp; TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...