I have a class defined as follow:
Mirf.h
class Mirf{
public:
void init(void);
};
Mirf.cpp
#include <Mirf.h>
Mirf mirf = Mirf();
Mirf::init(){
[Some Code Here]
}
The class Wireless uses the Mirf instance:
Wireless.h
#include <Mirf.h>
class Wireless {
public:
void init(void);
};
Wireless.cpp
#include <Wireless.h>
#include <Mirf.h>
Wireless::init(){
mirf.init();
}
My main method:
Main.cpp
#include <Wireless.h>
#include <Mirf.h>
Wireless wireless = Wireless();
int main(){
wireless.init();
}
The code compiles normally but the [Some Code Here] in Mirf.cpp doesn't called:
What's is wrong in my code?
Just because code is #included doesn't mean that it will be called. Only the main function is called automatically, any other functions need to be referenced inside expressions or they won't run.
Related
Consider Main.cpp:
//Main.cpp
#include "Main.h"
int main(){
class SIMPLE smpl;
smpl.setval(10);
smpl.setname();
smpl.printname();
}
and Main.h thus:
//Main.h
#pragma once
#include <iostream>
#include <string>
#include <vector>
class SIMPLE {
public:
int retval() const { return val; }
void setval(int i) { val = i; }
void setname();
void printname();
private:
int val;
std::string name;
};
The member functions setname() and printname() are missing definitions. VSCode Intellisense does not seem to be able to warn the user about this. See figure below which does not have any squiggles:
Visual Studio IDE, on the other hand, is able to point out the missing definitions via squiggles and allows for creation of the definition in the implementation file directly from the header file. See gif image below:
Is there a way to have VSCode cpptools extension provide these features via some settings, perhaps?
I've been programming a Monopoly game for a final project. So I thought I was on a roll, and that I had everything figured out with my psuedocode. But, it seems I forgot how to deal with includes properly, I know that is the issue since I was able to refine it to that point, but I'm not sure how to fix it.
In this super stripped down version of my code I have three .h files "Space.h" which is an abstract/virtual class which has to be inherited by a variety of different spaces that can appear on a typical Monopoly board: properties, jail, taxes, Chance, Community Chest, etc. The function that has to be inherited is run(Player&) which is what is "run" when you land on that particular space on the board, all functions that use run use a player passed by argument.
#pragma once
#include <string>
#include "Player.h"
class Space
{
public:
virtual void run(Player&) = 0;
};
My second .h file is the "Property.h" this inherits from Space
#pragma once
#include "Space.h"
class Property : Space
{
public:
void run(Player&) override;
int i{ 0 };
};
Lastly I have the "Player.h" which has two variables a name and a vector of properties it owns.
#pragma once
#include <string>
#include <vector>
#include "Property.h"
class Player
{
public:
std::string name{ "foo" };
void addProperty(Property p);
private:
std::vector <Property> ownedProperties;
};
Here's a very basic Property implementation
#include "Property.h"
#include <iostream>
void Property::run(Player & p)
{
std::cout << p.name;
}
Player implementation
#include "Player.h"
#include <iostream>
void Player::addProperty(Property p)
{
ownedProperties.push_back(p);
}
And finally main
#include "Player.h"
#include "Space.h"
#include "Property.h"
int main()
{
Player p{};
Property prop{};
prop.run(p);
system("pause");
}
Every time this is run I get a slew of errors, I'm sure it's got to do something with the circular include logic, with player including property, and property including space, which includes player. But, I don't see a workaround considering #include is needed to know how everything is defined isn't? Or are these errors referring to something else?
You have a circular include problem. Player includes Property which includes Space which includes Player again.
You can break the circle by not including Player.h in Space.h and only forward declare the class
#pragma once
class Player;
class Space
{
public:
virtual void run(Player&) = 0;
};
Aaand im back again with my second question and im kinda not sure about wether i should have posted all the seperate classes cuz it looks somewhat long. And im sure the solution is pretty small.
Anyways, i am at polymorphism tutorial vid that i am following and everything works fine if i follow it and put all classes in "main.cpp". But when i tried to do the same program with seperate classes (seen below) i am getting error "
E:\Codeblocks\Poly\main.cpp|11|error: cannot convert 'Ninja' to 'Enemy*' in initialization|".*
I kinda understand what the error is saying..i think.. but dont know what i did wrong since the same code was working when Enemy and Ninja class wasnt seperate but now as seperate classes its not working. I think i included those classes properly in main.cpp.
main.cpp
#include <iostream>
#include "Enemy.h"
#include "Ninja.h"
#include "Monster.h"
int main()
{
Ninja n;
Monster m;
Enemy *enemy1=&n;
Enemy *enemy2=&m;
enemy1->setAttackPower(20);
enemy2->setAttackPower(50);
n.attack();
m.attack();
return 0;
}
Enemy.h
#ifndef ENEMY_H
#define ENEMY_H
class Enemy
{
public:
Enemy();
void setAttackPower(int a);
protected:
int attackPower;
private:
};
#endif // ENEMY_H
Enemy.cpp
#include "Enemy.h"
Enemy::Enemy()
{
//ctor
}
void Enemy::setAttackPower(int a)
{
attackPower=a;
};
Ninja.h
#ifndef NINJA_H
#define NINJA_H
class Ninja
{
public:
Ninja();
void attack();
protected:
private:
};
#endif // NINJA_H
Ninja.cpp
#include "Ninja.h"
#include <iostream>
Ninja::Ninja()
{
//ctor
}
void Ninja::attack(){
std::cout<<" I am a ninja. Ninja chop! -"<<attackPower<<"\n";}
This is because your Ninja class is not inhereted from Enemy class. You must define Ninja class like this:
#include "Enemy.h"
class Ninja : public Enemy
{
public:
Ninja();
void attack();
protected:
private:
};
EDIT: I added #include directive. Without it compiler won't know, where to find Enemy class declaration.
I have a problem and I hope you can help me.
I'm creating a C++ program which is running on linux.
I have defined two classes, where the main one is called Downloader and looks like this:
#ifndef __DOWNLOADER_H__
#define __DOWNLOADER_H__
#include "configDownloader.h"
#include "mailServer.h"
#include <logger.h>
using namespace ObjectModel;
namespace Downloader
{
class Downloader
{
private:
...
MailServer *m_mailServer;
public:
Downloader(char* configFileName);
...
};
}
#endif
In the constructor of this class, I have tried to create a new instance of the class MailServer, which I defined into the same Namespace. The code of MailServer looks this way:
#ifndef __MAILSERVER_H__
#define __MAILSERVER_H__
#include <stdio.h>
#include <list>
#include <mail.h>
using namespace std;
using namespace ObjectModel;
namespace Downloader
{
class MailServer
{
private:
list<Mail> m_mails;
char *m_username;
char *m_password;
public:
MailServer();
~MailServer();
void Open(char *username,char *password);
bool SaveEmails(char *pathFiles);
void Close();
};
}
#endif
The constructor of this class is defined correctly into the .cpp file and everything seems correct.
The problem is that when I try to create a new instance of MailServer inside the constructor of Downloader, the compiler says "error: expected a type"
#include <stdio.h>
#include "downloader.h"
#include <unistd.h>
namespace Downloader
{
Downloader::Downloader(char* fileName)
{
this->m_config = new ConfigDownloader(fileName);
this->m_log = new Logger("Log",LOG_LEVEL_INFO,0);
this->m_mailServer = new MailServer();//the compiler shows the error right here
}
...
any ideas? I read somewhere that it could be that the compiler is too old, but I don't feel really comfortably coding inside the makefile.
I found the solution! I'm sorry because i didn't see it. The problem was that i defined a public getter to return the private field like this:
class Downloader
{
private:
ConfigDownloader* m_config;
Logger* m_log;
MailServer *m_mailServer;
public:
MailServer *MailServer();
being that both was define into the same scope, the compiler could be confused about the constructor of the class and the method, because both were called with the same name. The problem was mine! But everyone should take care about this, because intellisense doesn't tells you anything about it
what am I doing wrong?
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "fileoperations.h"
namespace Ui {
class MainWindow;
}
class FileOperations;
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
FileOperations FileController;
private slots:
void on_OpenButton_clicked();
void on_SaveButton_clicked();
void on_EncodeButton_clicked();
void on_DecodeButton_clicked();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
When i try to compile and run the program, it says:
g:\ke\c++ projects\projects\qt\shitlencoder\mainwindow.h:18: error: C2079: 'MainWindow::FileController' uses undefined class 'FileOperations'
Here's the strange thing, if I change 'FileOperations FileController;' to 'FileOperations *FileController;'(Obviously this compiles wrongly, because the rest of my codes that you can't see havn't been adapted to '->' instead of '.')
Then if I change it back to 'FileOperations FileController;' it lets me compile the program once (And it works fine), then it has the error the next time I try to compile it.
I'm using Qt 5.0.
fileoperations.h:
#ifndef FILEOPERATIONS_H
#define FILEOPERATIONS_H
#include "ui_mainwindow.h"
#include "mainwindow.h"
#include <QFileDialog>
#include <string>
#include <time.h>
#include <iostream>
#include <conio.h>
#include <windows.h>
#include <fstream>
using namespace std;
class FileOperations
{
public:
FileOperations();
void SetInputFile(QString x);
void SetOutputFile(QString x);
void EncryptAndSave(Ui::MainWindow *NUI);
void DecryptAndSave(Ui::MainWindow *NUI);
void createid(int id, int id2);
int GetCFuncion();
void SetCFuncion(int x);
long long Get_Size(string filename);
bool Get_Toobig(string path);
//DWORD WINAPI Thread_no_1();
private:
string InputFilename;
string OutputFilename;
int CFuncion;//CurrentFunction;
vector<int> conbyte1;
vector<int> conbyte2;
vector<int> opbyte1;
vector<int> opbyte2;
vector<int> passwordbytes;
};
#endif // FILEOPERATIONS_H
I assume that, in your .cpp file, you are using
#include "fileoperations.h"
Then, in fileoperations.h, you are including mainwindow.h which again includes fileoperations.h which is basically correct, since you are using a FileOperations object as parameter. But, due to the guards, class FileOperations is not seen by the compiler this time, hence FileOperations is unknown when used as parameter in your method. You need to break this dependency:
In fileoperations.h, use a forward declaration for Ui::MainWindow and remove the #include "mainwindow.h":
namespace Ui {
class MainWindow;
}
...
Since you are holding a FileOperations object in your class, you need the full class declaration. This means you have to include the header, you cannot simply forward declare the class like you are doing now. If you hold only a pointer, and do not have any code in your header that attempts to dereference the pointer, then the forward declaration is enough.
EDIT You have a cyclical include. You are including mainwindow.h in fileoperations.h. You can fix if by removing that include completely.
You have circular include issue, mainwindow.h and fileoperations.h include each other, try to remove below line from fileoperations.h
#include "mainwindow.h"