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!
Related
i have 2 .h files:
first.h
namespace first
{
void foo() { std::cout << 1 << ","; }
void bar() { std::cout << 7; }
}
second.h
namespace second
{
void foo() { std::cout << 3 << ","; }
void bar() { std::cout << 9; }
}
and the main.cpp
#include <iostream>
#include "first.h"
#include "second.h"
......................
void main() {
foo();
bar();
}
The goal is that the main prints 3,7 using these h files above.
Does someone have an idea how I could do it? Thanks
You can call the functions using the namespace:
second::foo();
first::bar();
But if you want to use the same functions over and over you can bring them into your scope via
using second::foo;
using first::bar;
foo();
bar();
I don't know exactly why this is happening but it seems that the code is "losing" the value of an attribute. I got a class defined as (in Foo.h):
#ifndef FOO_H
#define FOO_H
using namespace std;
#include <string>
class Foo{
public:
Foo(short int id);
void Foo::DoSomething(std::string someMsg);
private:
short int id;
};
#endif /* FOO_H */
in the class implementation (Foo.cpp):
#include "Foo.h"
#include <iostream>
Foo::Foo(short int id) {
this->id = id;
cout << "value now: " << this->id << "\n"; // I print to be sure it was set correctly and it prints the right value
}
void Foo::DoSomething(std::string someMsg) {
cout << "Foo number: " << std::to_string(this->id) << someMsg;
// when it runs this code called by another class, it always prints 0 to this->id for all objects instantiated
}
Then, I got another class, Bar. In Bar.h I have:
#ifndef BAR_H
#define BAR_H
#include <iostream>
using namespace std;
#include <list>
#include "Foo.h"
class Bar {
public:
Bar();
void setValor(std::string valor);
void notifyAllFoos();
void Bar::registerFoo(Foo *h);
private:
std::list<Foo> foos;
};
#endif /* BAR_H */
In Bar.cpp I have:
#include "Bar.h"
void Bar::setValor(std::string valor){
// do more stuff
this->notifyAllFoos();
}
void Bar::registerFoo(Foo *h){
this->foos.push_back(*h);
}
void Bar::notifyAllFoos(){
for(std::list<Foo>::iterator it=this->foos.begin() ; it!=this->foos.end() ; it++){
it->DoSomething("myMsg");
}
}
Finally, in main.cpp:
#include <cstdlib>
#include "Bar.h"
#include "Foo.h"
using namespace std;
int main(int argc, char** argv) {
Bar *b = new Bar();
b->registerFoo(new Foo(1));
b->registerFoo(new Foo(2));
b->registerFoo(new Foo(3));
b->setValor("Value");
delete b;
return 0;
}
Basically, Bar must notify all Foos of it's list that an update of an Value occured and print a Msg. To identify each Foo I put this id but it keeps printing 0 for all.
Why is this happening? It must be something very simple I guess but I'm not used to c++. Thanks in advance.
That:
std::list<Foo> foos;
will store copies of objects from class Foo.
You need to store pointers, like this:
std::list<Foo*> foos;
PS: Never forget to delete for every new, when you are done.
I did take your code and put it into one file, added includes and using at the top and commented out declaration of Bar constructor.
#include <string>
#include <iostream>
using namespace std;
class Foo{
public:
Foo(short int id);
void Foo::DoSomething(std::string someMsg);
private:
short int id;
};
Foo::Foo(short int id) {
this->id = id;
cout << "value now: " << this->id << "\n"; // I print to be sure it was set correctly and it prints the right value
}
void Foo::DoSomething(std::string someMsg) {
cout << "Foo number: " << std::to_string(this->id) << someMsg;
// when it runs this code called by another class, it always prints 0 to this->id for all objects instantiated
}
#include <list>
class Bar {
public:
//Bar();
void setValor(std::string valor);
void notifyAllFoos();
void Bar::registerFoo(Foo *h);
private:
std::list<Foo> foos;
};
void Bar::setValor(std::string valor){
// do more stuff
this->notifyAllFoos();
}
void Bar::registerFoo(Foo *h){
this->foos.push_back(*h);
}
void Bar::notifyAllFoos(){
for(std::list<Foo>::iterator it=this->foos.begin() ; it!=this->foos.end() ; it++){
it->DoSomething("myMsg");
}
}
int main () {
Bar *b = new Bar();
b->registerFoo(new Foo(1));
b->registerFoo(new Foo(2));
b->registerFoo(new Foo(3));
b->setValor("Value");
}
It gives me following output
value now: 1
value now: 2
value now: 3
Foo number: 1myMsgFoo number: 2myMsgFoo number: 3myMsg
Is it what you are looking for?
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;
}
As you can see, the title is a little nonsense, it's because I really don't know how to call that, but know that I tried hard to find a better title.
I hope you understand what I'm trying to say seeing the rest of the question.
Let's suppose that I have 3 classes, "A", "B", and "C".
The class "A" includes "B" and "C".
The class "B" includes "A"
The class "C" includes "A"
Let's go the code...
#include <iostream>
#include <b.hpp>
#include <c.hpp>
class A
{
public:
A()
{
std::cout << "Hello from A" << std::endl;
A a;
B b;
}
};
#include <iostream>
#include <a.hpp>
class B
{
public:
B()
{
std::cout << "Hello from B" << std::endl;
}
};
#include <iostream>
#include <a.hpp>
class C
{
public:
C()
{
std::cout << "Hello from C" << std::endl;
}
};
This way, everything works fine, the output:
Hello from A
Hello from B
Hello from C
BUT, if I do that:
#include <iostream>
#include <a.hpp>
#include <vector>
class B
{
public:
B()
{
std::cout << "Hello from B" << std::endl;
}
private:
std::vector<A> garage;
};
I got a cascade of errors, including this one (actually, the main one, because of that one, there are others):
error: 'A' was not declared in this scope
std::vector garage;
This is exactly what I want, do you have an idea what I can do? Thank you.
#edit - As answer to #Kerrek SB
I tried to create separated files to each one, header and sources. (.hpp and .cpp) and the error persist.
A.hpp
#ifndef A_HPP
#define A_HPP
#include <iostream>
#include <B.hpp>
#include <C.hpp>
class A
{
public:
A();
};
#endif
A.cpp
#include <A.hpp>
A::A()
{
std::cout << "Hello from A" << std::endl;
B b;
C c;
}
B.hpp
#ifndef B_HPP
#define B_HPP
#include <iostream>
#include <A.hpp>
#include <vector>
class B
{
public:
B();
private:
std::vector<A> garage;
};
#endif
B.cpp
#include <B.hpp>
B::B()
{
std::cout << "Hello from B" << std::endl;
}
C.hpp
#ifndef C_HPP
#define C_HPP
#include <iostream>
#include <A.hpp>
class C
{
public:
C();
};
#endif
C.cpp
#include <C.hpp>
C::C()
{
std::cout << "Hello from C" << std::endl;
}
main.cpp
#include <iostream>
#include <A.hpp>
int main(int argc, char ** argv)
{
A a;
return 0;
}
gcc
g++ -O2 -std=c++11 -I"." -o exchange A.cpp B.cpp C.cpp main.cpp
output
In file included from ./A.hpp:5:0,
from ./C.hpp:5,
from C.cpp:1:
./B.hpp:13:17: error: 'A' was not declared in this scope
std::vector<A> garage;
Separate the class definitions from the class member definitions:
#include <iostream>
#include <vector>
class A { public: A(); };
class B { public: B(); private: std::vector<A> garage; };
class C { public: C(); };
A::A()
{
std::cout << "Hello from A" << std::endl;
// A a; // no, because that's idiotic
B b;
}
B::B()
{
std::cout << "Hello from B" << std::endl;
}
C::C()
{
std::cout << "Hello from C" << std::endl;
}
In a larger project, you would probably have separate header and source files for each of those.
Please excuse me but I didn't know to give a name to the title in a short way.
Why do I need to declare an overloaded operator inside the header to make it work in this example:
HEAD.H
#pragma once
namespace test {
class A {
public:
A() : x(0) {}
int x;
};
A& operator++(A& obj); //this is my question
}
HEAD.CPP
#include "head.h"
namespace test {
A& operator++(A& obj) {
++obj.x;
return obj;
}
}
MAIN.CPP
#include <iostream>
#include "head.h"
using namespace std;
using namespace test;
int main() {
A object;
++object; //this won't work if we delete declaration in a header
return 0;
}
operator++ is defined and declared in a namespace inside "head.cpp" so why do I need to declare it one more time in a header?
Thank you.
The CPP files are compiled independently of each other, and they only see the header files they've included (which are in fact textually added to the source code of the CPP before compilation). As such you'll use the header file to inform the compiler that a function exists with that signature (be it an operator overload).
Then the output from your CPP files is put together by the linker, which is when you'd find out if for instance you had declared a function in a header file but never taken the trouble to implement it.
Simple example with namespaces:
#include <iostream>
namespace test{
int f() { return 42; }
int g() { return -1; }
}
namespace other{
int f() { return 1024; }
}
using namespace other;
int main(){
//error: 'g' was not declared in this scope
//std::cout << g() << std::endl;
std::cout << test::f() << std::endl; //42
std::cout << f() << std::endl; //1024
return 0;
}