Undefined reference to 'WinMain#16' - c++

I am getting a [Linker Error] undefined reference to 'WinMain#16' and I am unable to fix the issue. I am using Dev-C++ - In my project settings 'Win32 Console' is selected as I want it to be a console application.
Example Header (Test.h):
#ifndef TEST_H
#define TEST_H
#include<string>
using namespace std;
class Test {
private:
int testing;
public:
int main();
};
#endif
Example .cpp file
#include<iostream>
#include "Test.h"
using namespace std;
int Test::main(){
/* EXAMPLE */
cout << "Enter Test" <<endl;
cin >> testing;
cout << "----------------------------"<<endl;
system("pause");
return 0;
}
I can fix the error by removing the Test:: in front of the main() but I want it to reference my header file. If it does not reference my header file all of my variables become undeclared.. unless I put them into the program itself.
Please note the code is only an example of what I am doing.. and sorry once again if it's stupidly obvious. :-(

Answers are provided in the comments themselves, but here is the gist::
#ifndef TEST_H
#define TEST_H
#include<string>
using namespace std;
class Test {
private:
int testing;
public:
int main();
};
int Test::main(){
/* EXAMPLE */
cout << "Enter Test" <<endl;
cin >> testing;
cout << "----------------------------"<<endl;
system("pause");
return 0;
}
#endif
And in the .cpp file::
#include<iostream>
#include "Test.h"
using namespace std;
int main(){
/* EXAMPLE */
Test *testObject = new Test();
testObject->main();
delete(testObject);
system("pause");
return 0;
}

Oh, and why are you using system("PAUSE"), when there is a much better way!? (You can read here as to why system() is evil: http://www.cplusplus.com/forum/articles/11153/ )
Why not go with something like this:
void PressEnterToContinue()
{
std::cout << "Press ENTER to continue...\n";
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
and then call the function at the end??

Related

invalid operands to binary expression ('std::__1::ostream' (aka 'basic_ostream<char>') and 'void')

i am using eclipse on mac to run c++ program. I am new to c++ and was trying to learn composition by using different classes individually.I am facing the issue in the following line of the code
Main.cpp
#include <iostream>
using namespace std;
#include "Birthday.h"
#include "People.h"
int main() {
cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!!
Birthday obj(25,3,1993);
obj.print();
People pp(5,obj);
pp.printinfo();
return 0;
}
Birthday.cpp
#include <iostream>
using namespace std;
#include "Birthday.h"
//#include "People.h"
Birthday::Birthday(int d,int m,int y){
// TODO Auto-generated constructor stub
date =d;
month=m;
year=y;
}
void Birthday::print()
{
cout <<date << month<<year<<endl;
}
People.h
#ifndef PEOPLE_H_
#define PEOPLE_H_
//using namespace std;
#include "Birthday.h"
class People {
public:
People(int x,Birthday bb);
void printinfo();
private:
int xx;
Birthday bo;
};
#endif /* PEOPLE_H_ */
People.cpp
#include "People.h"
#include <iostream>
using namespace std;
#include "Birthday.h"
#include<string>
People::People(int x,Birthday bb)
:xx(x),bo(bb)
{
// TODO Auto-generated constructor stub
}
void People::printinfo()
{
cout<< xx<<bo.print(); //I am getting error because of this line , as soon as i comment it program compiles fine.
}
I have tried to use string variable instead of xx variable but it was giving me some other error.So i tried to simplify and learn the concept of compostion before jumping into strings manipulation directly.
cout << xx << bo.print();
bo.print() - function and they not have return value (void)
Just write:
cout << xx;
bo.print();

'cout' is not a member of 'std' (Progect File c++)

I created a new project in c++ but i keep getting the same error
Main.cpp
#include <iostream>
#include <string.h>
#include "Computer.cpp"
#include "Computer.h"
using namespace std;
int main()
{
cout << "Hello World!" << endl;
return 0;
}
Computer.h
#ifndef COMPUTER_H_INCLUDED
#define COMPUTER_H_INCLUDED
#include <string>
class Computer
{
public:
std::string marca;
float prezzo;
bool acceso;
Computer();
void Accenditi();
void Spegniti();
void ImpostaMarca(std::string m);
void ImpostaPrezzo(float p);
};
#endif
Computer.cpp
#include "Computer.h"
Computer::Computer()
{
}
void Computer::Accenditi()
{
if(!acceso)
{
acceso = true;
}
else
{
std::cout << "Sono già acceso";
}
}
void Computer::Spegniti()
{
if(acceso)
{
acceso = false;
}
else
{
std::cout << "Sono già spento";
}
}
void Computer::ImpostaMarca(std::string m)
{
marca = m;
}
void Computer::ImpostaPrezzo(float p)
{
prezzo = p;
}
The Problem
i don't understand what's wrong with Computer.cpp, i keep getting "cout is not a member of std". I tryed to add "using namespace std" and i also tryed to add the library #include but i get a new file called "makefile.win". How can i fix this error ?
You need to include iostream header in your Computer.cpp file as such:
include <iostream>
and to make your life easier, you can also add:
using std::cout;
using std::endl;
right at the bottom of your include, that way you don't have to keep adding "std::cout" everytime, you can just use "cout"
Also want to add:
You can remove the include computer.cpp from your main.cpp and just leave the header. The C++ linker will automatically link your computer.h and computer.cpp together since .cpp includes the header, and your main includes the computer.h
Add # include <iostream> at the files that you use std::cout and std::cin.

Run-Time Check Failure #2 - Stack around the variable 'l1' was corrupted

I am currently testing out creating a simple class which sets a number to a private variable, following the last tutorial for deconstructors in this link:
https://www.tutorialspoint.com/cplusplus/cpp_constructor_destructor.htm
However, I have been running into this particular issue where it says my variable is corrupted.
Run-Time Check Failure #2 - Stack around the variable 'l1' was corrupted.
This error happens only at the end Training.cpp, when it reaches the last curly bracket.
Here I defined the Line.cpp class alongside the headers.
//Line.cpp
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
class Line {
public:
void setLength(double len);
double getLength(void);
Line();
private:
double length;
};
Line::Line(void){
cout << "Object is being created." << endl;
}
void Line::setLength(double len) {
length = len;
}
double Line::getLength(void) {
return length;
}
// Line.h
#pragma once
#ifndef LINE_H
#define LINE_H
class Line
{
public:
Line();
void setLength(double len);
double getLength(void);
};
#endif LINE_H
And Training.cpp calls main function which calls the Line class
// Training.cpp
#include "stdafx.h"
#include <string>
#include <iostream>
#include "Line.h"
using namespace std;
int main()
{
Line l1;
l1.setLength(10.0);
cout << "Length of line: " << l1.getLength() << endl;
return 0;
}
The only difference between the tutorial and my version is that I have extracted the class Line and put it in a class file called Line, which is called by Tranining which has the main function in it.
I have extensively searched for other versions of this error but most of it mentions that it seems to be some form overstepping array boundaries. However I have not assigned any form of char array and am entirely clueless as to why the error is happening. Could any of you help me out on this?
Thanks!
You defined twice Line class.
Once in Line.h and another time in Line.cpp.
They also differs (in Line.h it hasn't the member double length; )
Your Line files shoud be like this:
//Line.cpp
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
Line::Line(void){
cout << "Object is being created." << endl;
}
void Line::setLength(double len) {
length = len;
}
double Line::getLength(void) {
return length;
}
// Line.h
#pragma once
#ifndef LINE_H
#define LINE_H
class Line
{
public:
Line();
void setLength(double len);
double getLength(void);
private:
double length;
};
#endif LINE_H
Have followed Federico's answer and the error disappeared. Thanks for the patience!
Just had to add the header file into the Line.cpp and it worked.

C++ Code::Blocks not compiling with header files

Good evening! (morning?)
I was wondering if anyone is familiar with the following issues. There are three files here, which are Cat.cpp, Cat.h, and CatMain.cpp. The issues are as follows:
When I try to build Cat.cpp, I get the error "undefined reference to WinMain#16".
When I try to build CatMain.cpp, I get undefined reference errors for the speak and jump functions.
The files are in the same folder and the code is just one-liners:
Cat.cpp
#include <iostream>
#include "Cat.h"
using namespace std;
void speak()
{
cout << "meow" << endl;
}
void jump()
{
cout << "meow?" << endl;
}
Cat.h
#ifndef CAT_H
#define CAT_H
void speak();
void jump();
#endif // CAT_H
CatMain.cpp
#include <iostream>
#include "Cat.h"
using namespace std;
int main()
{
speak();
jump();
return 0;
}
Is there anything wrong with this code? Or is anyone aware of whether or not this is a Code::Blocks or a compiler issue?
Any help is greatly appreciated =)
All that your code does as of now is something like this :
Cat.cpp ::
#include <iostream>
using namespace std;
void speak();
void jump();
void speak()
{
cout << "meow" << endl;
}
void jump()
{
cout << "meow?" << endl;
}
CatMain.cpp ::
#include <iostream>
using namespace std;
void speak();
void jump();
int main()
{
speak();
jump();
return 0;
}
You Cat.cpp has a main method missing because of which it wouldn't compile.
Your CatMain.cpp does not have any definition for speak() and jump(), hence undefined error.
Point : CatMain.cpp doesn't know what Cat.cpp is trying to work out.
int main() { return 0; }
added to Cat.cpp should let it compile.
void speak(){
cout << "defined" << endl;
}
void jump(){
cout << "defined" << endl;
}
added to CatMain.app should work for it as well.
Your code is fine.
Most probably you don't want to compile separate files but project as whole. You should add both cpp to project in your IDE and linker will link them together resolving both issues.

Multiple definition of function in C++

I am having a very unusual problem:
I keep getting multiple definition of functions in my class.
This is my main .cpp
#include <iostream>
#include "Calculation.cpp"
using namespace std;
int main()
{
cout << "Hello world!" << endl;
return 0;
}
this is my class .h
#ifndef CALCULATION_H_INCLUDED
#define CALCULATION_H_INCLUDED
class Calculation
{
public:
Calculation();
private:
};
#endif // CALCULATION_H_INCLUDED
this is my implementation file .cpp
#include "Calculation.h"
Calculation::Calculation()
{
}
Please help me; I have tried to create a new project but that didn't help.
All help is appreciated.
make your main.cpp like :
#include <iostream>
#include "Calculation.h" // not Calculation.cpp
using namespace std;
int main()
{
cout << "Hello world!" << endl;
return 0;
}
You have to include your Calculation.h in th main.cpp and you have to compile it as follows,
g++ main.cpp Calculate.cpp -o main -I<path for your .h file>
main.cpp
#include<iostream>
#include "Calculation.h"
//using namespace std; // Avoid this, always to use std::cout .. etc on place
int main()
{
Calculation c; //Creating the object of Calculation class
std::cout<<"Hello World!"<<std::endl;
return 0;
}