No output to the console? - c++

I'm trying to learn C++ OOP and thought would try a simple example using Qt Creator. I clicked New Project > Projects > application > Qt Console Application
I then added a new class, test.cpp. Both test.cpp and main.cpp are in the Sources folder and test.h is in the Headers foler.
Here is test.h
#ifndef TEST_H
#define TEST_H
class test
{
public:
test();
};
#endif // TEST_H
test.cpp
#include "test.h"
#include "iostream"
using namespace std;
test::test()
{
cout<<"Inside test's constructor "<<endl;
}
main.cpp
#include "iostream"
#include "test.h"
using namespace std;
int main()
{
test ts;
return 0;
}
When I click the run button, it's built and run. The console window displays but "Inside test's constructor" never gets to be printed to the screen. What am I doing wrong?

Go to Preferences in Qt Creator, select Environment. You will see two boxes under the General Tab. They are User Interface and System.
Under System you will see
Terminal :
Mine says
/usr/X11/bin/xterm -e
Try a different terminal from the one you have now. This has been a problem on some systems for a while.

I stopped bothering about dealing with cout, the console, etc and just use QDebug
qDebug() << "Date:" << QDate::currentDate();
qDebug() << "Types:" << QString("String") << QChar('x') << QRect(0, 10, 50, 40);
qDebug() << "Custom coordinate type:" << coordinate;
#include "test.h"
#include "iostream"
#include <QDebug>
using namespace std;
test::test()
{
qDebug()<<"Inside test's constructor ";
}

Related

Unable to jump to function definition after opening C++ project in VSCode

I use VSCode to remotely open a project on Ubuntu through remote ssh on Windows.
My project consists of only three files: a1.h, a1.cc, and main.cc. When I first opened the project, I was unable to jump to the definition of the print function in a1.h by clicking F12.
However, if I opened a1.cc and tried again to jump to the definition of print in a1.h, it worked correctly.
a1.h
#ifndef A1_H
#define A1_H
class A1 {
public:
void print();
};
#endif
a1.cc
#include "a1.h"
#include <iostream>
void A1::print()
{
std::cout << "hello world";
}
main.cc
#include "a1.h"
int main() {
A1 obj;
obj.print();
}
I tried two ways, but they didn't work.
1.Configure Files::associations in VSCode settings
2.Configure fs.inotify.max_user_watches = 524288

Unable to compile a C++ class using code blocks

Everytime I try to compile a class in c++ I get this error:
||=== Build file: "no target" in "no project" (compiler: unknown) ===|
Here is the code for my Classes class:
#include <iostream>
#include "Cat.h"
using namespace std;
int main() {
Cat cat1;
cat1.speak();
cat1.jump();
return 0;
}
Here is the code for my header Cat.h:
#ifndef CAT_H_
#define CAT_H_
class Cat {
public:
void speak();
void jump();
};
#endif /* CAT_H_ */
And here is the code for my Cat Class:
#include <iostream>
#include "Cat.h"
using namespace std;
void Cat::speak() {
cout << "Meouwww!!!" << endl;
}
void Cat::jump() {
cout << "Jumping to top of bookcase" << endl;
}
This error have nothing to do with your code. It's a problem related to your environnement. There is 2 commun mistake that will lead to this:
There is no compiler associated with your IDE so try to install one. Or you should Download codeBlocks with mingw compiler integrated
You didn't create a project So try creating a project and then add this files.
I hope that I answered your question.

C++ class in separate file not compiling. Already defined in Class.obj one or more multiply defined symbols found

So I've done extensive googling and searching on StackOverflow and am unable to find a solution despite several answers with this exact issue.
I am trying to create a test class in an external file called Fpc5.cpp
It's contents are:
Fpc5.cpp
#include "stdafx.h"
#include "Fpc5.h";
#include <iostream>
using std::cout;
class Fpc5 {
int bar;
public:
void testMethod();
};
void Fpc5::testMethod() {
cout << "Hey it worked! ";
}
and my main .cpp file:
Test.cpp
// Test.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "iostream"
//#include "Fpc5.cpp"
#include "Fpc5.h";
using std::cout;
using std::cin;
using std::endl;
int main()
{
cout << "Hello" << endl;
Fpc5 testObj;
testObj.testMethod();
system("pause");
return 0;
}
all the answers I've read indicate this is caused becaused I used to be including the class in the main file itself which is why I created a header file
Fpc5.h
#pragma once
void testMethod();
This changed the error, but still did not fix the issue. Currently my Test.cpp does not recognize a Fpc5 class. I've also tried adding the Fpc5.cpp and Fpc5.h in stdafx.h and that still does not resolve the issue.
stdafx.h
// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently, but
// are changed infrequently
//
#pragma once
#include "targetver.h"
#include <stdio.h>
#include <tchar.h>
// TODO: reference additional headers your program requires here
//#include "Fpc5.cpp"
#include "Fpc5.h"
I'm sure this a simple syntax/conceptual understanding error, but I'm quite new to c++ and am not sure what is wrong.
This is definition of your class and it must be in Fpc5.h
class Fpc5 {
int bar;
public:
void testMethod();
};
Then, you have Fpc5.cpp where you implement methods of the class:
#include "Fpc5.h" // Compiler needs class definition to compile this file!
void Fpc5::testMethod()
{
}
And then you can use Fpc5 class in Test.cpp
#include "Fpc5.h"
int main()
{
Fpc5 foo;
foo.testMethod();
return 0;
}
As an alternative you can pack everything into Test.cpp
Move the definition of your class:
class Fpc5 {
int bar;
public:
void testMethod();
};
to the header file, "Fpc5.h".
Implement the methods to "Fpc5.cpp".

how to Separate Files Classes in Xcode

I am new C++ and I have a question about separate files classes in Xcode. I did write a program trying to lean class, but I got an error. Could anyone teach me how to do that right ?
I include the program that I tried below
this is the (main CCP):
#include <iostream>
#include "hhhhhhhhhhhhhhhh.h"
using namespace std;
int main ( int argc, char ** argv )
{
bassam bo;
bo.bassamfunction();
}
this is the (.h) :
#ifndef __try_some_concspte__hhhhhhhhhhhhhhhh__
#define __try_some_concspte__hhhhhhhhhhhhhhhh__
#include <iostream>
class bassam{
public:
void bassamfunction();
};
#endif /* defined(__try_some_concspte__hhhhhhhhhhhhhhhh__) */
this is the (CCP):
#include <iostream>
#include "hhhhhhhhhhhhhhhh.h"
using namespace std;
bassam::bassamfunction()
{
cout << " heloo I am here "<< endl ;
}
Your problem is that bassam::bassamfunction() has no return type in the .cpp file. This should be changed to void bassam::bassamfunction().

C++ Error while compiling a new class

I've been trying to use de CodeBlocks IDE, and when a create a new class, I got an error at the beggining of the code:
Class.cpp :
#include "Class.h" // Error (exactly the same message as I get in main.cpp
#include <iostream>
Class::Class()
{
//ctor
}
Class.h
#ifndef CLASS_H
#define CLASS_H
class Class
{
public:
Class();
protected:
private:
};
#endif // CLASS_H
main.cpp
#include <iostream>
#include "Class.h" // this line show me an error
using namespace std;
int main()
{
cout << "Hello world!" << endl;
return 0;
}
This message appears on the console fatal error: Class.h: No such file or directory
Directory of cpp classes : Workspace/Test/Sources/src/Classes
Directory of h class : Workspace/Test/Headers/include/class
I tried to change to #include "Headers/include/Class.h" bud didn't work out
Any help is appreciate, bye.