Search code examples
c++visual-studio-2010classheaderlnk2019

A LNK 2019 Error with my class I created


I have beeen using this template to make my classes to store data for various parts of my program.

//Classs that DOES NOT work
//"MiningPars.h"
#pragma once
#ifndef MININGPAR_H
#define MININGPAR_H
#include <iostream>
#include <fstream>
using namespace std;
class MiningPars
{
public:
    int NumYears;
    double iRate;
    int MaxMineCap;
    int MaxMillCap;
    int SinkRate;
    double ReclaimCost;
    double PitSlope;

public:
    MiningPars();

    MiningPars(int numYears,double irate,int maxMineCap,int maxMillCap,
        int sinkRate, double reclaimCost, double pitSlope): NumYears(numYears),
        iRate(irate),MaxMineCap(maxMineCap),MaxMillCap(maxMillCap),SinkRate(sinkRate),
        ReclaimCost(reclaimCost),PitSlope(pitSlope) {}
            };
            #endif

when I just declare a new mining par, it gives me the error

1>main.obj : error LNK2019: unresolved external symbol "public: __thiscall MiningPars::MiningPars(void)" (??0MiningPars@@QAE@XZ) referenced in function "void __cdecl `dynamic initializer for 'par''(void)" (??__Epar@@YAXXZ) 

i.e. my code looks like this:

    #include "MiningPars.h"

    MiningPars par;//Error
    vector <PredecessorBlock> PREDS;//is okay
    void main()
    {
        par.iRate = .015;
        //... etc.  
    }

most MSDN and other google searches say that i am not declaring things correctly or i haven't added appropriate dependanies, but it is the same format that I created another class. an example of my other work can be seen here:

//Classs that works
#pragma once

#ifndef PREDECESSOR_H
#define PREDECESSOR_H
#include <string>
#include <vector>
#include <algorithm>
#include <sstream>
#include <iostream>
#include <string>
#include <fstream>

using namespace std;


class PredecessorBlock
{
public:
    int BLOCKID;
    vector <int> PREDS_IDS;
    int PredCount;

public:
    PredecessorBlock();

    PredecessorBlock(int blockID,vector<int>predsids,
        int predcount) : BLOCKID(blockID), 
        PREDS_IDS(predsids), PredCount(predcount)
    {}


};

#endif

so this has been confusing for me. I appreciate any advice you may give


Solution

  • When you write

    MiningPars par;
    

    a par object of type MiningPars is created on the stack. To create an object, the constructor (in this case the default one, that is the one with no arguments) is called, but you don't define the constructor in your MiningParsclass.

    To solve this, either define the constructor in your class cpp file, or if the constructor doesn't need to do anything, you can write the empty inline definition in the header file:

    class MiningPars
    {
    public:
        MiningPars() {}
    ...
    };
    

    or just don't declare the default constructor, the compiler will generate a empty one for you.