Undefined reference to function in class - c++

main.cpp:
#include <iostream>
#include "pokemonList.h"
void pokemonLookup();
int main() {
pokemonLookup();
return 0;
}
void pokemonLookup() {
pokemonList pL;
std::cout<<std::endl<<"What Pokemon do you want to look up? ";
std::string pokemonLookup;
std::cin>>pokemonLookup;
pL.displayPokemon(pokemonLookup);
}
pokemonList.h:
#ifndef POKEMONLIST_H
#define POKEMONLIST_H
#include <iostream>
class pokemonList
{
private:
struct pokemonTemplate {
std::string pokemonName;
std::string pokemonMoves[3];
int pokemonLevel;
int baseATK;
int baseDEF;
int baseSPATK;
int baseSPDEF;
int baseSPEED;
};
pokemonTemplate bulbasaur;
pokemonTemplate pikachu;
public:
void displayPokemon(std::string pokemon);
protected:
};
#endif // POKEMONLIST_H
pokemonList.cpp:
#include "pokemonList.h"
/*
pokemonTemplate* bulbasaur() {
bulbasaur.pokemonName = "Bulbasaur";
bulbasaur.pokemonMoves[3];
bulbasaur.pokemonLevel = 5;
bulbasaur.baseATK = 10;
bulbasaur.baseDEF = 10;
bulbasaur.baseSPATK = 10;
bulbasaur.baseSPDEF = 10;
bulbasaur.baseSPEED = 10;
}
pokemonTemplate* pikachu() {
pikachu.pokemonName = "Pikachu";
pikachu.pokemonMoves[3];
pikachu.pokemonLevel = 5;
pikachu.baseATK = 8;
pikachu.baseDEF = 10;
pikachu.baseSPATK = 12;
pikachu.baseSPDEF = 6;
pikachu.baseSPEED = 15;
}
*/
void displayPokemon(std::string pokemon) {
std::cout<<std::endl<<"Looking up: " + pokemon<<std::endl;
}
I realize in the pokemonList.cpp file I have a bunch of Bad Code commented out, that isn't what this question is about. When I try to compile, I get a single error in the main.cpp saying:
D:/CodeBlocks/Projects/RelearningCPlusPlus/main.cpp:15: undefined reference to `pokemonList::displayPokemon(std::string)'

You defined a function displayPokemon() but it isn't defined as a member function. To define it as a member function outside the class definition, you need to mention the class name:
void pokemonList::displayPokemon(std::string pokemon) {
std::cout<<std::endl<<"Looking up: " + pokemon<<std::endl;
}

it should be
void pokemonList::displayPokemon(std::string pokemon){....

You need to change this:
void displayPokemon(std::string pokemon) {
to this:
void pokemonList::displayPokemon(std::string pokemon) {

Dietmar's answer solves the problem.
But I want to add that it is better to passing argument by reference instead of passing by value with std::string pokemon in this case, thus reducing overhead. So the displayPokemon function should be:
void pokemonList:displayPokemon(std::string &pokemon) const {
std::cout<<std::endl<<"Looking up: " + pokemon<<std::endl;
};
Also, remember to change the corresponding member function declaration.

displayPokemon function is declared as a pokemonList member function but is never defined.
This:
void displayPokemon(std::string pokemon) {
std::cout<<std::endl<<"Looking up: " + pokemon<<std::endl;
}
is a definition of some other function (with the same name) which is not a pokemonList class member function. To tell the compiler that this is a class pokemonList member function you must preceed its name with pokemonList::
void pokemonList::displayPokemon(std::string pokemon) {
std::cout<<std::endl<<"Looking up: " + pokemon<<std::endl;
}
Alternatively, you can define displayPokemon inside pokemonList class:
class pokemonList
{
private:
struct pokemonTemplate {
std::string pokemonName;
std::string pokemonMoves[3];
int pokemonLevel;
int baseATK;
int baseDEF;
int baseSPATK;
int baseSPDEF;
int baseSPEED;
};
pokemonTemplate bulbasaur;
pokemonTemplate pikachu;
public:
void displayPokemon(std::string pokemon) {
std::cout<<std::endl<<"Looking up: " + pokemon<<std::endl;
}
protected:
};
The difference is that in the last case the displayPokemon will be an inline function.

Related

How do I access a static member of class inside an inner class?

#include <iostream>
using namespace std;
class outer {
public :
int a=10; //non-static
static int peek; //static
int fun()
{
i.show();
cout<<i.x;
}
class inner {
public :
int x=25;
int show()
{
cout<<peek;
}
};
inner i;
int outer::inner::peek=10; //here is the problem
};
int main()
{
outer r;
r.fun();
}
So this is the code. I need to give a value to the static integer.
As I compile, it gives me an error. I am currently a beginner and still learning.
Can someone explain this?
You can define the static variable outside the class definition:
#include <iostream>
class outer {
public:
int a = 10; //non-static
static int peek; //static
void fun() {
i.show();
std::cout << i.x << '\n';
}
class inner {
public:
int x = 25;
void show() {
std::cout << peek << '\n';
}
};
inner i;
};
int outer::peek = 10; // <----------- here
int main() {
outer r;
r.fun();
}
Or define it inline:
class outer {
public:
int a = 10; //non-static
static inline int peek = 10; //static inline
// ...
Note: I changed your member functions to void since they don't return anything.
Output:
10
25

calling a struct member declared in a class

I am trying to place functions inside a struct which is part of a class (here named menu) so I can modify the struct inside of a dedicated setup cpp file (I am trying to do this so I can modify all of the functions I want in my application in a single source file instead of changing stuff in all of my cpp files):
// Menu.h
class menu
{
public:
menu();
struct pages
{
void print_page_1();
void print_page_2();
};
};
// Setup.cpp
struct menu::pages
{
void print_page_1()
{
// ...
}
void print_page_2()
{
// ...
}
};
Then I get an error when trying to call a function within my struct:
int main()
{
menu myMenu();
myMenu.pages.print_page_1(); // error: type name is not allowed
}
What does this error mean and how can I avoid it?
Thank you!
pages is the name of the struct, it's not an object. You need an object of type pages inside menu.
Otherwise, you can have static methods inside pages and call those without creating objects.
Example (live):
#include <iostream>
struct S
{
struct P
{
void print()
{
std::cout << "Hello from P!\n";
}
} p; // object of P
struct Q
{
static void print()
{
std::cout << "Hello from Q!\n";
}
};
};
int main()
{
S s;
s.p.print();
S::Q::print();
return 0;
}
Output:
Hello from P!
Hello from Q!
You need to declare a pages object in menu.
#include <iostream>
class menu
{
std::string p1 = "1";
std::string p2 = "2";
public:
struct pages
{
menu& m;
pages(menu &m):m(m){
}
void print_page_1();
void print_page_2();
} pages;
menu():pages(*this){
}
};
void menu::pages::print_page_1()
{
std::cout << m.p1;// ...
}
void menu::pages::print_page_2()
{
std::cout << m.p2;// ...
}
int main() {
menu myMenu;
myMenu.pages.print_page_1();
// your code goes here
return 0;
}

undefined reference to my constructor

I have a simple class which I cannot instantiate and I don't know why...
Please help me !
-------Test.cpp-------
#include<iostream>
using namespace std;
#include "meteo.h"
int main()
{
Meteo meteo;
}
-------meteo.h---------
#ifndef METEO_H
#define METEO_H
class Meteo
{
public:
Meteo();
int Get(int i);
private:
char *list[];
};
#endif
-------meteo.cpp--------
#include "meteo.h"
Meteo::Meteo()
{
list[]("Sec","Venteux","Humide");
}
int Meteo::Get(int i)
{
return list[i];
}
I get the error: "undefined reference to `Meteo::Meteo()'"
It seems that the problem is that the compiler issued an error when was compiling the constructor
Meteo::Meteo()
{
list[]("Sec","Venteux","Humide");
}
and did not generate the object module.
This record
list[]("Sec","Venteux","Humide");
is invalid.
Try to change the class definition like
class Meteo
{
public:
Meteo();
int Get(int i);
private:
const char *list[3];
};
and define the constructor like
Meteo::Meteo() : list { "Sec","Venteux","Humide" }
{
}
The other reason might be that you did not include object module meteo in the project.
Take into account that this member function
int Meteo::Get(int i)
{
return list[i];
}
is also wrong. The type of elements of the array is const char * not int.

Simple Polymorphism cast not working

I have a class SourceComponent, and its derived class, PeriodicSourceComponent.
Implementations are:
class SourceComponent : public Component
{
protected:
friend class UserInterface;
friend void readInput();
public:
virtual int returnType();
virtual int propagateLogic();
virtual void sourcePropagation(float);
virtual void accept(VisitorSources&);
SourceComponent();
};
and
#include "source_component.h"
class PeriodicSourceComponent : public SourceComponent
{
private:
int frequency;
friend void main();
friend void readInput();
friend class UserInterface;
public:
void sourcePropagation(float);
int returnType();
PeriodicSourceComponent();
};
When I try in some different class/method to do:
SourceComponent* s = new PeriodicSourceComponent;
it won't let me, sayin "a value of type periodicblabla cant be assigned to value of type sourceblabla". Why?
Edit:
Ok, in my main it looks lke this:
#include "source_component.h"
#include "periodic_source_component.h"
void main()
{
SourceComponent* s = new PeriodicSourceComponent;
}
And implementations of both classes:
source.cpp:
#include "source_component.h"
SourceComponent::SourceComponent()
{
outputState = -1;
}
int SourceComponent::propagateLogic()
{
return 1;
}
int SourceComponent::returnType()
{
return 5;
}
and periodic.cpp
#include "periodic_source_component.h"
PeriodicSourceComponent::PeriodicSourceComponent()
{
outputState = 0;
}
int PeriodicSourceComponent::returnType()
{
return 3;
}
void PeriodicSourceComponent::sourcePropagation(float time)
{
float t = time, period;
period = 1000000/frequency;
if(t > period)
{
while(t >= period)
t -= period;
}
if(t <= (period/2))
outputState = 0;
else
outputState = 1;
}
and its not working... (outputState is a member of class Component, base class of SourceComponent)
and the error message: A value of type "PeriodicSourceComponent*" cannot be assigned to a value of type "SourceComponent*".
Important Edit
When I try to compile, the actual compiler error is at PeriodicSourceComponent declaration, it says: "Base class undefined".
and also, I have two other derived classes from SourceComponent, but I don't see how they could interfere with this one..
EDIT 4
Ok so I figured out what causes the error, you were right of course, it something else I didn't post. I have class VisitorSources, heres definition:
#ifndef __VISITOR_SOURCES_H__
#define __VISITOR_SOURCES_H__
#include "component.h"
#include "impulse_source_component.h"
#include "arbitrary_source_component.h"
#include "periodic_source_component.h"
class VisitorSources
{
protected:
VisitorSources();
public:
virtual void visitImpulseSource(ImpulseSourceComponent*);
virtual void visitArbitrarySource(ArbitrarySourceComponent*);
virtual void visitPeriodicSource(PeriodicSourceComponent*);
void visitSource(int, float);
};
#endif
And its implementation is not yet written:
#include "visitor_sources.h"
void visitSource(int type, float time)
{
}
void VisitorSources::visitArbitrarySource(ArbitrarySourceComponent* a)
{
}
When I comment out the entire Visitor class and implementation, the above-mentioned errors are gone for some reason. I have no idea why...
The only error that remains is that when I try to use s->frequency, it says that frequency is not a member of SourceComponent, which is true, but it is a member of PeriodicSourceComponent, which is why I used the cast in the first place..
Finally, here's the Component class, the main class for all almost all other classes in the project :P
#ifndef __COMPONENT_H__
#define __COMPONENT_H__
#include <iostream>
#include <vector>
class Component
{
friend class UserInterface;
friend void readAndSimulate();
protected:
Component();
int numOfInputs;
int numOfOutputs;
std::vector<Component*> inputs;
std::vector<Component*> outputs;
friend void main();
float lengthOfSimulation;
int typeIfSource;
public:
int outputState;
virtual int propagateLogic() = 0;
virtual int returnType();
int beginPropagation();
virtual void sourcePropagation(float);
~Component();
};
#endif
And implementation:
#include "component.h"
#include <conio.h>
Component::Component()
{
}
int Component::beginPropagation()
{
std::vector<Component*>::const_iterator iter = outputs.begin();
for(;iter<outputs.end();++iter)
{
if ((*iter)->outputState == -2)
{
(*iter)->outputState = outputState;
return (*iter)->outputState;
}
}
std::vector<Component*>::const_iterator it = outputs.begin();
int finishedCycle, x;
while(1)
{
finishedCycle = 1;
for(; it < outputs.end(); ++it)
{
x = (*it)->propagateLogic();
if(!x)
finishedCycle = 0;
}
if(finishedCycle) break;
it = outputs.begin();
}
it = outputs.begin();
for(;it<outputs.end();++it)
(*it)->beginPropagation();
}
int Component::returnType()
{
return 0;
}
void Component::sourcePropagation(float)
{
}
Component::~Component()
{
std::vector<Component*>::const_iterator it = inputs.begin();
for(; it < inputs.end(); ++it)
{
if((*it) != NULL)
{
delete *it;
Component* p = *it;
p = NULL;
}
}
it = outputs.begin();
for(; it < inputs.end(); ++it)
{
if((*it) != NULL)
{
delete *it;
Component* p = *it;
p = NULL;
}
}
}
Are you using include guards in all your header files?
Not having them can cause the kinds of problems you're seeing.
Three guesses:
You copied and pasted code between header files and forgot to change the #include guards.
You use precompiled headers and included something before the #include "stdafx.h".
If you use precompiled headers, try deleting the .pch file.

C++ class with static pointer

I don't understand pointers and references very well yet, but I have a class with static methods and variables that will be referenced from main and other classes. I have a variable defined in main() that I want to pass to a variable in this class with static functions. I want those functions to change the value of the variable that is seen in the main() scope.
This is an example of what I am trying to do, but I get compiler errors...
class foo
{
public:
static int *myPtr;
bool somfunction() {
*myPtr = 1;
return true;
}
};
int main()
{
int flag = 0;
foo::myPtr = &flag;
return 0;
}
Provide the definition of the static variable outside the class as:
//foo.h
class foo
{
public:
static int *myPtr; //its just a declaration, not a definition!
bool somfunction() {
*myPtr = 1;
//where is return statement?
}
}; //<------------- you also forgot the semicolon
/////////////////////////////////////////////////////////////////
//foo.cpp
#include "foo.h" //must include this!
int *foo::myPtr; //its a definition
Beside that, you also forgot the semicolon as indicated in the comment above, and somefunction needs to return a bool value.
#include <iostream>
using namespace std;
class foo
{
public:
static int *myPtr;
bool somfunction() {
*myPtr = 1;
return true;
}
};
//////////////////////////////////////////////////
int* foo::myPtr=new int(5); //You forgot to initialize a static data member
//////////////////////////////////////////////////
int main()
{
int flag = 0;
foo::myPtr = &flag;
return 0;
}