Codeblocks Console Application Settings: Undefined reference to WinMain - c++

Edit with respect to solution suggestion:
The solution for the author of the question was that he put the main function into a namespace. Then he added parameters to the main function, then he used extern "C" in order to get his main to be recognized as entry point.
Answers, which looked detailed suggested that the code author did not define a main/win main function (which was debated). I also don't use Visual C++, but a mingw version which works on my AMD computer (its a win 64 variant on a windows 10 home edition).
I have a problem, which seems frequently to find its way to StackOverflow and I could not locate any answer which helped me resolve my problem.
I used codeblocks and made a project as a console application.
This is the code I tried to compile and I keep on getting the "undefined WinMain" error.
I used the win64 MinGW compiler, which came with codeblocks
Compiler Flags were: (-std=C++11, -std=C++14, and -std=C++17. I used all of previous one at a time, before), -Wall, and -pedantic
This is the code and naturally, I was not able to resolve my problem. The attached images are "Build log" and "Build Messages" from the respective codeblock tabs of the "Logs & other window".
Please help.
#include <iostream>
using namespace std;
class Rectangle {
protected:
int width, height;
public:
Rectangle(int width = 0, int height = 0): width(width), height(height) {};
int get_width() const {return width;}
int get_height() const {return height;}
virtual void set_width(int width) {this->width = width;}
virtual void set_height(int height) {this->height = height;}
int area() const {return width * height;}
};
class Square : public Rectangle {
public:
Square(int size = 0) : Rectangle(size, size) {
set_width(size);
this->width = this->height = size;
}
void process(Rectangle &r){
r.set_height(10);
cout << "expected area was 30, got " << r.area() << std::endl;
}
int main() {
Rectangle r(3,4);
process(r);
std::cout << "Template" << std::endl;
return 0;
}
};

Possibly this is part of the solution:
I know that extra compiler flags can be created.
This is a warning, which showed, after I wrapped my main function with extern "C" as suggested by one of the answers in your solution suggestion
||warning: command line option '-std=c17' is valid for C/ObjC but not for C++|.
In my case I chose the option, where it said -std=c++17, still this warning came up.

Related

extra qualification error in DevC++ IDE with TDM-GCC 4.9.2

I have a class function that is defined as follows:
class Output
{
private:
window* pWind;
public:
Output();
window* CreateWind(int, int, int, int);
void CreateDesignToolBar(); //Tool bar of the design mode
void CreateSimulationToolBar();//Tool bar of the simulation mode
window * getwindow()const;
void CreateStatusBar();
void CreateDrawArea();
Input* CreateInput(); //creates a pointer to the Input object
void ClearStatusBar(); //Clears the status bar
void ClearDrawArea(); //Clears the drawing area
void DrawAssign(Point Left, int width, int height, string Text, bool Selected = false);
void Output::Drawcondition(Point left, int width, int height, int t_width, int t_height, string Text, bool Selected = false);
When I compile the source in DevC++, I get:
33 7 C:\Users\user\source\repos\flowchart-designer-and-simulator\GUI\Output.h [Error] extra qualification 'Output::' on member 'Drawcondition' [-fpermissive]
What is this? How do I remove this error?
First of all, Dev-C++ is not a compiler, but an IDE (a fancy editor, simply said). It uses some kind of other compiler under the bonnet. Proabably gcc (from MINGW), I don't really remember, as Dev-C++ is quite dated tool.
Secondly, you have not given full code (EDIT: full code was added later), but based on the error I think you have declared a method inside of class and qualified it with that class name. This is incorrect, as qualification there is not needed.
I.e. you should do something like this:
class Test {
void test ();
};
Not something like this (which, I presume, you have tried):
class Test {
void Test::test ();
};

Different betwee {} and = when C++11 initialize variables

I used CLion as an IDE, it reports an error in IDE as
field z must be initialized
It can compile and run. But if I change const int z{3}; to const int z=3;, no error will be reported in IDE. My question is whether it is indeed an error of my codes or it is just a bug in the IDE? Any difference between these two initialization approaches? Did your IDE report this error?
#include <iostream>
using namespace std;
class Test
{
private:
const int x = 3;
int y;
const int z{3};
public:
Test(int);
int gety(){
return y;
}
};
Test::Test(int a){
y=x+4;
}
int main()
{
Test test(5);
std::cout << test.gety() << std::endl;
return 0;
}
whether it is indeed an error of my codes
There is no error in the code, it is OK.
or it is just a bug in the IDE?
It is a bug in whatever generates the error message. The IDE is high on my list of suspects but it could be another tool whose message the IDE relays.
Any difference between these two initialization approaches?
In this context (default member initializer) both syntaxes are semantically equivalent. There is no difference.

Use g++ or CC to compile multiple C++ files

Good Day,
I have tried to lookup how to compile several C++ files on a *nix command line.
I've tried these two links
Using G++ to compile multiple .cpp and .h files
Using G++ to compile multiple .cpp and .h files
I have a simple abstract class:
// Base class
class Shape
{
public:
// pure virtual function providing interface framework.
virtual int getArea() = 0;
void setWidth(int w) {
width = w;
}
void setHeight(int h) {
height = h;
}
protected:
int width;
int height;
};
Then a derived one:
// Derived classes
class Rectangle: public Shape
{
public:
int getArea()
{
return (width * height);
}
};
Here is the driver:
#include <iostream>
using namespace std;
int main(void)
{
Rectangle Rect;
Rect.setWidth(5);
Rect.setHeight(7);
// Print the area of the object.
cout << "Total Rectangle area: " << Rect.getArea() << endl;
return 0;
}
This is a simple one, so I don't need a makefile, but this is what I've tried:
> g++ Shape.cc - This creates a Shape.o
> g++ Shape.cc Rectangle.cc ShapeDriver.cc - This creates an error
> g++ ShapeDriver.cc Shape.cc Rectangle.ccc - This creates an error
It turns out that Rectangle.cc is not recognizing the width and height definitions, which makes sense.
What else do I need to do to get this to compile? I'm a total C++ noob.
TIA,
coson
You need to add the following to the different files...
Top of Rectangle.cc
#include "Shape.cc"
Top of ShapeDriver.cc
#include "Rectangle.cc"
Also, in your third gcc line, you have a typo
g++ ShapeDriver.cc Shape.cc Rectangle.ccc - This creates an error
It should have been Rectangle.cc
What your problem is, is that in each of your files, the different classes have never been defined, so they don't know how to use them. Like.... "Rectangle" first needs to know what a "Shape" is before it derives from it. You should be using .h files in between w/ the class definitions, and including them in the other .cc files so they know the other class structures they are calling on.

"Expected Class name" Error for Inheritance

Problem
Currently designing a GUI game, and I've finished the basic OOP aspects of the game(along with 90% of all non-abstract classes). However, I tried extending a class called Protester to this class which caused the error at line 5 here:
#ifndef HardcoreProtester_h
#define HardcoreProtester_h
#include "Protester.h"
class HardcoreProtester : public Protester{
public:
HardcoreProtester(StudentWorld* w, int x, int y) : Protester(w, x, y, IID_HARD_CORE_PROTESTER, 20){};
private:
};
#endif /* HardcoreProtester_h */
when extending from this
#ifndef Protester_h
#define Protester_h
#include "Actor.h"
#include "StudentWorld.h"
class Protester : public Human{
static const int INIT_PERP_TICK = 200;
static const int DAMAGE = 20;
static const int SHOUT_WAIT = 25;
static const int MIN_STEP = 8;
static const int MAX_STEP = 60;
static const int EXIT_X = 60;
static const int EXIT_Y = 60;
public:
static const int INIT_HITPOINTS = 5;
Protester(StudentWorld* w, int startX, int startY, int ID, int hp);
virtual ~Protester();
virtual void doSomething();
Direction pickRandomDirection();
virtual bool changeState(Direction dir);
virtual bool isDead() const{
return Human::isDead() && getX() == 60 && getY() == 60;
}
virtual bool isDeadState() const{
return Human::isDead();
}
virtual void consume();
virtual void setDead();
virtual bool moveDelta(StudentWorld* world, Direction dir, int& xdir, int& ydir, int steps = 1);
int determineRandomSteps();
bool canTurn(Actor::Direction dir);
Actor::Direction randTurn(Actor::Direction dir);
Actor::Direction oppositeDir(Actor::Direction dir);
Actor::Direction numToDir(int num);
private:
int step;
int restTick;
int shoutTick;
int perpTick;
};
#endif /* Protester_h */
I've looked on stack overflow for answers to why the error persists, and I've tried to break a nonexistent circular dependency (as you can see Protester does not even include HardcoreProtester). I tried to break any circular dependency by adding a
class Protester;
above the definition of HardcoreProtester.
However, this gives me the error:
Type 'Protester' is not a direct or virtual base of 'HardcoreProtester'
and
Base class has incomplete type
I also made sure the base class is not abstract(I was able to initialize it without any errors).
If this isn't enough information, here's the github for the project:
https://github.com/OneRaynyDay/FrackMan
I apologize for any ambiguity in my question - I just simply have no idea where the error could be(hence an attempt for MCVE with github link). Thanks in advance!
EDIT: Also, using XCode to make this project. By this point into debugging I'm starting to suspect XCode of being the culprit.
In addition, just to be extra helpful and verifiable, here's a picture of the error diagnosis from XCode:
No, XCode is not the culprit. It's not XCode's fault that you have circular header dependencies.
According to the compiler dump you posted, it appears that your StudentWorld.h header file has a #include of HardProtester.h.
This is a case of classical circular header dependencies.
First, you are including Protester.h.
Before Protester.h even gets to its definition of the Protester class, it has an #include of StudentWorld.h.
StudentWorld.h must have an #include of HardProtester.h, according to your compiler's error diagnostics.
Now, your HardProtester.h has it's own include of Protester.h. But, because its ifndef/define guard has already been set, in the first include of Protester.h, the second #include of this header file becomes empty text.
And now, upon returning to HardProtester.h, you attempt to declare it's class.
Now, if you have been paying attention carefully, you should've figured out that the Protester class has not yet been declared, yet this header file attempts to declare its subclass.
There's your problem. You need to completely refactor how your header files depend on each other, to eliminate this circular dependency. Merely sticking a "class Protester" in HardProtester.h is insufficient. The entire class must be defined, not just declared, before you can declare any subclasses.
You have a circular dependency between Protester.h and StudentWorld.h
Try fixing that and see if it helps.

Getting "multiple types in one declaration" error in C++

Can anyone tell me why i get a "Block.h:20: error: multiple types in one declaration" error message when compiling this file. Nothing seems to be solving this problem and I'm getting pretty frustrated.
Displayable.h
#include <X11/Xlib.h>
#include <X11/Xutil.h>
// Information to draw on the window.
struct XInfo
{
Display *display;
Window window;
GC gc;
};
// An abstract class representing displayable things.
class Displayable
{
public:
virtual void paint(XInfo &xinfo) = 0;
};
Sprite.h
#include "Displayable.h"
enum Collision {
NO_COLLISION = 0,
TOP_COLLISION,
RIGHT_COLLISION,
BOTTOM_COLLISION,
LEFT_COLLISION
};
class Sprite : public Displayable {
public:
int x, y, width, height;
Sprite();
virtual void paint(XInfo &xinfo) = 0;
Collision didCollide(Sprite *s);
};
Block.h
#include "Sprite.h"
class Block : public Sprite {
public:
virtual void paint(XInfo &xinfo);
Block(int x, int y, int width, int height);
}; <-- **This is line 20**
As is, the code looks OK. I can't see anything wrong with the particular line. However, Xlib is a C library which likes to define quite a number of macros and conventionally uses CamelCase for macros, too. That is, I would suspect that something in your declarations actually happens to be a macro which gets expanded to something the C++ compiler doesn't like. To find this problem I recommend you use the omnipresent -E flag to have a look at the preprocessed source. That is, you'd remove any -o flag (and the name following it) and -c flag but otherwise you'd essentially retain the command line. The result will be written to standard output i.e. you want to redirect the output to more/less or some file.
Alternatively, you can go about and prefix all of your names by a prefix which is unlikely to be used in any of the X11 headers, e.g. parts of some name, offensive words, etc. I tried to reproduce the problem on my system but I didn't get an error message.