CPP Code Concerning Classes is Showing Error - c++

I wrote a pretty simple code for learning the concept of constructors.
I made a Project file in Dev-C++ 5.11, which uses the compiler TDM_GCC 4.9.2 32-bit Release.
Following is the code containing the main function:
#include <iostream>
#include "Classy1.h"
using namespace std;
int main()
{
Classy1 ao(3,4);
ao.printy();
return 0;
}
Here is the header file containing the class Classy1:
#ifndef CLASSY1_H
#define CLASSY1_H
class Classy1
{
private:
int v1;
int v2;
public:
Classy1(int a,int b);
void printy();
};
#endif
And here is the cpp file containing the constructor and the printy function:
#include "Classy1.h"
#include <iostream>
using namespace std;
Classy1::Classy1(int a,int b)
: v1(a), v2(b)
{
}
Classy1::printy()
{
cout << v1 << " " << v2;
}
Now, the compiler is showing an error on compilation:

You forgot to add a return type for the implementation of printy(). It should be:
void Classy1::printy()
{
cout << v1 << " " << v2;
}

Related

Simple Class and Header in Separate File Won't Work

I'm currently learning c++ for a week and here's my problem:
run.cpp
#include <iostream>
#include "Abc.h"
int main(){
int a;
std::cout << "Enter a : ";
std::cin >> a;
// Object Initialization
Abc AbcObj();
}
the header, Abc.h :
#ifndef ABC_H
#define ABC_H
class Abc
{
public:
Abc();
protected:
private:
};
#endif // ABC_H
and finally my cpp file for implementation, Abc.cpp:
#include "Abc.h"
#include <iostream>
Abc::Abc()
{
std::cout << std::endl << "Object created ";
}
Why don't I get output on my console? I'm expecting "object created" should be on the console. These files are in the same directory.
You error doesn't come up because you've used different files, so I have used one in this example
struct Foo
{
int a;
Foo()
{
std::cout << "Constructor called!";
}
};
int main()
{
Foo obj();
}
Why don't you see the message? You can read this thread
The problem here is, Foo obj() is taken as a function declaration. To fix this you need to remove the ()
int main()
{
Foo obj;
}
Constructor called!

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.

C2061: syntax error : identifier 'string' - Behaving weird

I am trying to learn C++, however, the parameter to a method I have in my own class is misbehaving. When it uses a dataType of 'int', it works fine with no errors, but when I attempt to change it to a 'string' dataType, the program crashes with this error.
Error 1 error C2061: syntax error : identifier 'string' in temp.h ln
8 col 1
The classes I am using are as follows:
WORKING CODE
TesterClass.cpp // Entry Point
#include "stdafx.h"
#include "Temp.h"
int _tmain(int argc, _TCHAR* argv[])
{
Temp tmp;
tmp.doSomething(7);
return 0;
}
Temp.h
#pragma once
class Temp
{
public:
Temp();
void doSomething(int blah);
};
Temp.cpp
#include "stdafx.h"
#include "Temp.h"
#include <iostream>
#include <string>
using std::string;
Temp::Temp()
{
std::cout << "Entry" << std::endl;
string hi;
std::cin >> hi;
std::cout << hi << std::endl;
}
void Temp::doSomething(int blah)
{
std::cout << blah;
}
BROKEN CODE
Temp.h
#pragma once
class Temp
{
public:
Temp();
void doSomething(string blah);
};
Temp.cpp
#include "stdafx.h"
#include "Temp.h"
#include <iostream>
#include <string>
using std::string;
Temp::Temp()
{
std::cout << "Entry" << std::endl;
string hi;
std::cin >> hi;
std::cout << hi << std::endl;
}
void Temp::doSomething(string blah)
{
std::cout << blah;
}
When I adjust the parameter 'blah' to be a string, in both the .h and .cpp file, the problem occurs.
I have looked around, but none of the answers seem to solve my problem. I would greatly love help on this an I am out of ideas. I have tried reinstalling C++, messing with:
using namepace std;
using std::string;
std::string instead of string
etc.
If you know how to solve my problem I would love to hear from you. I am more than happy to provide more information.
C++ performs single-pass compilation, so std::string needs to be declared before you use it at all - including in the header file.
// Temp.h
#pragma once
#include <string>
class Temp
{
public:
Temp();
void doSomething(std::string blah);
};
I would encourage you to be specific in your header files when specifying classes like this, because you might easily come across another library that defines it's own string and then you would run into naming conflicts. Save the using import statements for your cpp files.
πάντα ῥεῖ had the write answer, thankyou!
They said to use std::string when needed, and to also #include <string> in the header file.

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;
}