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
Related
I'm just learning C++. I'm following a YouTube video, but I'm stuck at this stage. Below is my files.
// main.cpp
#include <iostream>
#include "Person.h"
int main()
{
Person foo;
foo.set_age(25);
std::cout << foo.get_age() << std::endl;
return 0;
}
// Person.h
#ifndef PERSON_H
#define PERSON_H
#pragma once
class Person
{
public:
void set_age(int a);
int get_age();
private:
int age;
};
#endif
// Person.cpp
#include "Person.h"
void Person::set_age(int a)
{
age = a;
}
int Person::get_age()
{
return age;
}
I just set up the development environment in vscode and installed code runner.
Whenever I click the Run button here
It says
launch: program '.....main.exe' does not exist
Is there a tool like php composer that automatically manages it?
Moved the contents of the .cpp file to the .h file.
It worked fine. But I would like to separate the two files for management.
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.
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".
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 ";
}
I have already checked StackOverflow to find the solution to my problem, but I think I might be missing something. I am trying to define a class in a header file (.h) and implement its methods in a cpp file (.cpp), but it does not work.
main.cpp:
#include <iostream>
#include "Message.h"
using namespace std;
int main()
{
Message *t = new (Message);
t->display();
return 0;
}
Message.h:
#ifndef MESSAGE_H_INCLUDED
#define MESSAGE_H_INCLUDED
class Message {
public:
void display();
};
#endif // MESSAGE_H_INCLUDED
Message.cpp:
#include "Message.h"
void Message::display() {
cout << "Hello!";
}
I don't understand why I keep getting the following error
undefined reference to 'Message::display()'
Compile this with the command g++ -std=c++11 Message.cpp main.cpp