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
main.obj : error LNK2019: unresolved external symbol "public: __thiscall MiningPars::MiningPars(void)"
Linker is complaining about not providing default constructor definition for MiningPars.
class MiningPars
{
...
MiningPars(); // This is declaration.
// Have you forgotten to provide the definition of it
// in the source file ?
And
MiningPars par;
the above statement invokes default constructor. If the definition is empty then do -
MiningPars() {}
in the class definition like you did for the parameterized constructor.
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.
I agree with Mahesh
inside your class
MiningPars(){}
Related
Error C2079 'Message::simbolo' uses undefined class 'Symbol'
is generated on this line when compiling
Symbol simbolo;
This is my C++ code:
class Message
#pragma once
#include <string>
#include "Symbol.h"
#include "SharedEditor.h"
class SharedEditor;
class Symbol;
class Message
{
private:
SharedEditor* sender;
int action; //1 inserted 2 deleted
Symbol simbolo;
public:
Message();
Message(SharedEditor* sender, Symbol nuovosimbolo, int action);
~Message();
};
class Symbol
#pragma once
#include "SharedEditor.h"
#include <vector>
class SharedEditor;
class Message;
class Symbol
{
char character;
int siteID;
SharedEditor* generator;
std::vector<int> position;
public:
Symbol();
Symbol(char ch, SharedEditor* source, int ID, std::vector<int> pos);
~Symbol();
};
class SharedEditor:
#pragma once
#include "NetworkServer.h"
#include "Message.h"
#include "Symbol.h"
class Message;
class Symbol;
class SharedEditor
{
private:
NetworkServer& _server;
int _siteId;
std::vector<Symbol> _symbols;
//other functions
public:
SharedEditor(NetworkServer ns);
~SharedEditor();
void process(const Message& m);
};
class NetworkServer:
#pragma once
#include <iostream>
#include <vector>
#include <queue>
#include "SharedEditor.h"
#include "Message.h"
class SharedEditor;
class Message;
class NetworkServer
{
private:
std::vector<SharedEditor*> connected;
std::queue<Message> codaMessaggi;
public:
int connect(SharedEditor* sharedEditor);
void disconnect(SharedEditor* sharedEditor);
void send(const Message& m);
NetworkServer();
~NetworkServer();
};
You need to rework your various header files to break the dependency cycle.
The general rule is: if you only need pointers or references to a type T, you can get away with a forward declaration (class T;) instead of a full class declaration (class T { ... }, typically behind a #include).
For the example above I will go over each file and what you need:
Symbol.h needs a forward declaration of SharedEditor, as it only uses SharedEditor*.
Message.h needs a forward declaration of SharedEditor, but a full #include "Symbol.h" (compiler needs to know how big a Symbol is to calculate the size of Message)
SharedEditor needs a forward declaration of Message (passed as a reference), a full #include "NetworkServer.h" (passed as a parameter) and a full #include "Symbol.h" (used in a vector)
NetworkServer needs a forward declaration of SharedEditor (only used with pointers), but a full #include "Message.h" (used in a queue)
If you still have two classes that need full includes of each other, search stack overflow for "C++ dependency cycle" or somesuch.
I am trying to run a function from a class file, but it is not working and I get the the following error messages:
error LNK1120: 1 unresolved externals
error LNK2019: unresolved external symbol "public: void __thiscall NS::Class1::test(void)" (?test#Class1#NS##QAEXXZ) referenced in function _main
//Main.cpp
#include<iostream>
#include<string>
#include<Windows.h>
#include "Class1.h"
int main(){
NS::Class1 E;
E.test();
return 0;
};
//Class1.cpp
#include <Windows.h>
#include <string>
namespace NS{
class Class1{
Class1(){
OutputDebugString(L"Created.");
}
void test(){
OutputDebugString(L"Hello World");
}
};
}
//Class1.h
#ifndef _Class1_H_
#define _Class1_H_
namespace NS{
class Class1{
public:
void test();
};
}
#endif
In your source file, you're redefining the class, rather than defining its member functions. That will give undefined behaviour, since you're only allowed to define the class once. The source file should look more like:
#include "Class1.h"
#include <Windows.h>
NS::Class1::Class1(){
OutputDebugString(L"Created.");
}
void NS::Class1::test(){
OutputDebugString(L"Hello World");
}
You'll also need to modify the class definition in the header, since it doesn't declare a constructor.
Also, make sure that your project is compiling and linking both source files.
Your header file contains reserved IDs for the include guard and misses the declaration of the constructor. It should look like this:
#ifndef CLASS1_H
#define CLASS1_H
namespace NS {
class Class1
{
public:
Class1();
void test();
};
}
#endif
The definition of the class should not include the class declaration, it should include it, and should look more like this:
#include <Windows.h>
#include "Class1.h"
namespace NS {
Class1::Class1()
{
OutputDebugString(L"Created.");
}
void Class1::test()
{
OutputDebugString(L"Hello World");
}
}
I think your .cpp file might be causing the issue. Since you already created the class definition in your header with class member declarations, just import the Class1.h header in the Class1.cpp file and scope the member functions then defining their behavior.
so maybe try:
#include <Class1.h>
NS::Class1::Class1(){}//constructor
void NS::Class1::test(std::cout << "hi"){}
//class1.cpp
#include <iostream>
#include <stdlib.h>
using namespace std;
class class1
{
public:
int var;
class1(int i)
{
var = i;
}
};
//class1.h:
#include <iostream>
#include <stdlib.h>
using namespace std;
class class1
{
public:
int var;
class1(int i = 0);
};
//main.cpp
#include <iostream>
#include <stdlib.h>
#include "class1.h"
using namespace std;
int main()
{
class1 a(5);
return 0;
}
error:
1>main.obj : error LNK2019: unresolved external symbol "public: __thiscall class1::class1(int)" (??0class1##QAE#H#Z) referenced in function _main
what the heck is going on? I swear I've made almost the exact same program before and It's worked.
Change class1.cpp to something like:
//class1.cpp
#include "class1.h"
class1::class1(int i) : var(i) {}
You don't want to define the class itself again -- just define the member functions in the implementation file.
Then you'll build it something like:
g++ main.cpp class1.cpp
[of course, substituting the correct compiler name for the compiler you're using]
Most likely you are not compiling both cpp files. Check your build settings and make sure that both main.cpp and class1.cpp are being compiled.
However you also have a serious problem. You are declaring class1 twice. Once in the header:
class class1
{
public:
int var;
class1(int i = 0);
};
... and again in the cpp:
class class1
{
public:
int var;
class1(int i)
{
var = i;
}
};
In C++ there should be exactly one declaration for a class (and exactly one definition).
The way it works is you declare the class in your header, as you have already done, and then you define the members in the cpp, like this:
class1.cpp
#include "class1.h"
class1::class1(int i)
{
var = i;
}
Please have a look at the following code
Main.cpp
#include <iostream>
#include <string>
using namespace std;
int main()
{
system("pause");
return 0;
}
Magic.h
#pragma once
class Magic
{
public:
Magic();
~Magic();
virtual void display()=0;
};
Spell.h
#pragma once
#include "Magic.h"
#include <iostream>
#include <string>
using namespace std;
class Spell :
public Magic
{
public:
Spell(void);
Spell(string words);
~Spell(void);
void display();
private:
string words;
};
Spell.cpp
#include "Spell.h"
#include "Magic.h"
#include <iostream>
#include <string>
using namespace std;
Spell::Spell(void)
{
}
Spell::Spell(string words)
{
this->words = words;
}
Spell::~Spell(void)
{
cout << "Delete Spell" << endl;
}
void Spell::display()
{
cout << "Spell Words: " << words << endl;
}
Here, I am getting the error
1>------ Build started: Project: Revision1_1, Configuration: Debug Win32 ------
1>Spell.obj : error LNK2019: unresolved external symbol "public: __thiscall Magic::~Magic(void)" (??1Magic##QAE#XZ) referenced in function __unwindfunclet$??0Spell##QAE#XZ$0
1>Spell.obj : error LNK2019: unresolved external symbol "public: __thiscall Magic::Magic(void)" (??0Magic##QAE#XZ) referenced in function "public: __thiscall Spell::Spell(void)" (??0Spell##QAE#XZ)
1>C:\Users\yohan\Documents\Visual Studio 2010\Projects\Revision1_1\Debug\Revision1_1.exe : fatal error LNK1120: 2 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
I do not understand what to do here. Why is this happening? Please help! I am new to C++ anyway..
Magic is not implementing its constructor and destructor (which also should be virtual).
Don't even declare the constructor if not necessary, e.g.
class Magic {
public:
virtual ~Magic() {}
virtual void display() = 0;
};
Unrelated: I didn't know you can display magic.
You didn't implement
Magic();
~Magic();
You'll need to either implement them inline, in an implementation file, or mark them = default.
You have declared a destructor in your Magic class but did not define it. That's why the linker complains (and the compiler doesn't).
You don't have an implementation for Magic. If your intention is for Magic to be an abstract base class, then just change its declaration to:
#pragma once
class Magic
{
public:
virtual void display()=0;
};
Remember, any method that is not followed by = 0 in the interface must be implemented in the class.
This seems like a problem that is common. I defined an enum in classA and then included classA in classB. Then, in classB I defined a function which returns the enum type defined in classA...see below. I get the following error:
aFirst.obj : error LNK2019: unresolved external symbol "public: enum justEnum::things_t __thiscall usesTheEnum::returnsThings(void)" (?returnsThings#usesTheEnum##QAE?AW4things_t#justEnum##XZ) referenced in function _wmain
1>C:\Documents and Settings\Ben\My Documents\Visual Studio 2010\Projects\aFirst\Debug\aFirst.exe : fatal error LNK1120: 1 unresolved externals
#pragma once
class justEnum
{
public:
justEnum(void);
~justEnum(void);
enum things_t{ONE, TWO};
};
#pragma once
#include "justEnum.h"
class usesTheEnum
{
public:
usesTheEnum(void);
~usesTheEnum(void);
justEnum::things_t returnsThings(void);
};
#include "StdAfx.h"
#include "usesTheEnum.h"
#include "justEnum.h"
usesTheEnum::usesTheEnum(void)
{
}
usesTheEnum::~usesTheEnum(void)
{
}
justEnum::things_t returnsThings()
{
return justEnum::ONE;
}
// tester.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <stdio.h>
#include <iostream>
#include "justEnum.h"
#include "usesTheEnum.h"
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
usesTheEnum aUser;
justEnum anEnum;
justEnum::things_t anotherEnum;
anotherEnum = justEnum::ONE;
aUser.returnsThings();
cout << anotherEnum;
return 0;
}
You need to specify that your definition of returnsThings() is part of the usesTheEnum class.
justEnum::things_t usesTheEnum::returnsThings()
{
return justEnum::ONE;
}
error LNK2019: unresolved external symbol "public: enum justEnum::things_t __thiscall usesTheEnum::returnsThings(void)"
The compiler is complaining that usesTheEnum::returnThings() is not defined, and I cannot see a definition in the code you posted. You should provide a definition for the function in one translation unit.
I don't think I can emphasize enough how important it to learn to read error messages. The compiler is doing it's best to tell you what is wrong.
not shure, but can't you just move the enum out of the class?
Or on the .cpp of the class write something like
extern enum classname::things_t;
just to have the enum added to the generated lib wich is what will be linked against.