VSCode C++ : triggering creation of class member function definition automatically - c++

Consider Main.cpp:
//Main.cpp
#include "Main.h"
int main(){
class SIMPLE smpl;
smpl.setval(10);
smpl.setname();
smpl.printname();
}
and Main.h thus:
//Main.h
#pragma once
#include <iostream>
#include <string>
#include <vector>
class SIMPLE {
public:
int retval() const { return val; }
void setval(int i) { val = i; }
void setname();
void printname();
private:
int val;
std::string name;
};
The member functions setname() and printname() are missing definitions. VSCode Intellisense does not seem to be able to warn the user about this. See figure below which does not have any squiggles:
Visual Studio IDE, on the other hand, is able to point out the missing definitions via squiggles and allows for creation of the definition in the implementation file directly from the header file. See gif image below:
Is there a way to have VSCode cpptools extension provide these features via some settings, perhaps?

Related

Problem with includes, base class undefined C2504

This is my EngineIncludes.h file:
#include <stdafx.h>
//Engine Includes
namespace Engine
{
//Classes: 23
class BaseObject;
class Console;
class Engine;
class Node;
template <class T> class Transform;
}
#include "core/BaseObject.h"
#include "core/Console.h"
#include "core/Engine.h"
#include "math/Transform.h"
#include "scene/Node.h"
//Global Objects
extern Engine::Console* CONSOLE;
extern Engine::Engine* CORE_ENGINE;
In stdafx.h I have regular stuff like OpenGL, std::map, boost... and I'm building a precompiled header as standard.
This is the Node.h file:
#ifndef _NODE_H
#define _NODE_H
#include <stdafx.h>
#include <EngineIncludes.h>
namespace Engine
{
class Node : public BaseObject
{
public:
Node();
~Node();
void SetParent(Node* parent);
Node* GetParent();
void SetTransform(const Transform<double> &transform);
Transform<double> GetTransform();
Transform<double> GetDerivedTransform();
Transform<double> GetInheritedTransform();
void Update();
private:
Transform<float> transform;
Transform<float> derived;
Node* parent;
};
}
#endif _NODE_H
I get 3 errors here. One C2504 that Engine::BaseObject is not defined. And two C2079 that both Transform transform and Transform use undefined class. Now this is the BaseObject.h:
#ifndef _BASE_OBJECT_H
#define _BASE_OBJECT_H
#include "stdafx.h"
#include "EngineIncludes.h"
namespace Engine
{
class BaseObject
{
public:
BaseObject()
{
id = CORE_ENGINE->GenerateID();
}
~BaseObject()
{
}
long GetID()
{
return id;
}
private:
long id;
};
}
#endif _BASE_OBJECT_H
Now I specifically fully defined BaseObject inside header file with no luck. Still the same error. But if I comment out forward declarations in EngineIncludes.h, the compiler freaks out with
extern Engine::Console* CONSOLE;
It literally ignores all #includes for whatever reason. I've never had an issue like this before. And I tried everything. I even created EngineIncludes.cpp file. I moved the contents of EngineIncludes.h into stdafx.h with no luck. It somehow just ignores #includes. Even if I put #include "BaseObject.h" inside "Node.h" nothing changes. When it compiles Node it has both Transform which is a template class and BaseObject undefined, even though both objects are clearly defined before Node with forward declarations and #includes.
I'm using Visual Studio 2010, but never had an issue like this. I looked into my previous projects and all is written the same fashion and works without problem.

Swarmed with identifier errors

I've been programming a Monopoly game for a final project. So I thought I was on a roll, and that I had everything figured out with my psuedocode. But, it seems I forgot how to deal with includes properly, I know that is the issue since I was able to refine it to that point, but I'm not sure how to fix it.
In this super stripped down version of my code I have three .h files "Space.h" which is an abstract/virtual class which has to be inherited by a variety of different spaces that can appear on a typical Monopoly board: properties, jail, taxes, Chance, Community Chest, etc. The function that has to be inherited is run(Player&) which is what is "run" when you land on that particular space on the board, all functions that use run use a player passed by argument.
#pragma once
#include <string>
#include "Player.h"
class Space
{
public:
virtual void run(Player&) = 0;
};
My second .h file is the "Property.h" this inherits from Space
#pragma once
#include "Space.h"
class Property : Space
{
public:
void run(Player&) override;
int i{ 0 };
};
Lastly I have the "Player.h" which has two variables a name and a vector of properties it owns.
#pragma once
#include <string>
#include <vector>
#include "Property.h"
class Player
{
public:
std::string name{ "foo" };
void addProperty(Property p);
private:
std::vector <Property> ownedProperties;
};
Here's a very basic Property implementation
#include "Property.h"
#include <iostream>
void Property::run(Player & p)
{
std::cout << p.name;
}
Player implementation
#include "Player.h"
#include <iostream>
void Player::addProperty(Property p)
{
ownedProperties.push_back(p);
}
And finally main
#include "Player.h"
#include "Space.h"
#include "Property.h"
int main()
{
Player p{};
Property prop{};
prop.run(p);
system("pause");
}
Every time this is run I get a slew of errors, I'm sure it's got to do something with the circular include logic, with player including property, and property including space, which includes player. But, I don't see a workaround considering #include is needed to know how everything is defined isn't? Or are these errors referring to something else?
You have a circular include problem. Player includes Property which includes Space which includes Player again.
You can break the circle by not including Player.h in Space.h and only forward declare the class
#pragma once
class Player;
class Space
{
public:
virtual void run(Player&) = 0;
};

Get list of methods in class using clang

In common IDEs (pick one) you often have an outline view showing you the list of methods for a specific class.
Suppose I have a C++ interface class in IFoo.h that looks like this:
#ifndef IFOO_H_
#define IFOO_H_
class IFoo {
public:
virtual ~IFoo() {}
virtual void bar() = 0;
};
#endif
How (programmatically) can I get such an IDE outliner list for my IFoo.h file above using maybe the clang libraries? For a first start, it would help if I can get a list of names of functions.
I specifically intend to use clang, so any help on how to analyze the my header file with clang would be really appreciated.
Meanwhile I will have a look at the clang tutorial here: https://github.com/loarabia/Clang-tutorial
Thanks in advance for your help.
I went through this tutorial http://clang.llvm.org/docs/LibASTMatchersTutorial.html and found some pretty helpful stuff there, this is what I came up with:
I had to rename my file from IFoo.h to IFoo.hpp to be detected as Cxx and not C code.
I had to call my program with -x c++ to have my IFoo.h file being recognized as C++ code rather than C code (clang interprets *.h files as C by default:
~/Development/llvm-build/bin/mytool ~/IFoo.h -- -x c++
This is my code to dump all virtual functions from the provided class:
// Declares clang::SyntaxOnlyAction.
#include "clang/Frontend/FrontendActions.h"
#include "clang/Tooling/CommonOptionsParser.h"
#include "clang/Tooling/Tooling.h"
#include "clang/ASTMatchers/ASTMatchers.h"
// Declares llvm::cl::extrahelp.
#include "llvm/Support/CommandLine.h"
#include "clang/ASTMatchers/ASTMatchers.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include <cstdio>
using namespace clang;
using namespace clang::ast_matchers;
using namespace clang::tooling;
using namespace llvm;
DeclarationMatcher methodMatcher = methodDecl(isVirtual()).bind("methods");
class MethodPrinter : public MatchFinder::MatchCallback {
public :
virtual void run(const MatchFinder::MatchResult &Result) {
if (const CXXMethodDecl *md = Result.Nodes.getNodeAs<clang::CXXMethodDecl>("methods")) {
md->dump();
}
}
};
// CommonOptionsParser declares HelpMessage with a description of the common
// command-line options related to the compilation database and input files.
// It's nice to have this help message in all tools.
static cl::extrahelp CommonHelp(CommonOptionsParser::HelpMessage);
// A help message for this specific tool can be added afterwards.
static cl::extrahelp MoreHelp("\nMore help text...");
int main(int argc, const char **argv) {
cl::OptionCategory cat("myname", "mydescription");
CommonOptionsParser optionsParser(argc, argv, cat, 0);
ClangTool tool(optionsParser.getCompilations(), optionsParser.getSourcePathList());
MethodPrinter printer;
MatchFinder finder;
finder.addMatcher(methodMatcher, &printer);
return tool.run(newFrontendActionFactory(&finder));
}
The output looks like this, when passed the IFoo.h file:
CXXDestructorDecl 0x1709c30 <~/IFoo.h:5:3, col:20> ~IFoo 'void (void)' virtual
`-CompoundStmt 0x1758128 <col:19, col:20>
CXXMethodDecl 0x1757e60 <~/IFoo.h:6:3, col:24> bar 'void (void)' virtual pure

Calling parent's method

I'm kinda newbie to all this c++ stuff, so this probably is a beginner's problem:
ListScreen.h
#ifndef _LISTSCREEN_H_
#define _LISTSCREEN_H_
#include "MAUI/Screen.h"
namespace CoolPlaces {
namespace Views {
using namespace MAUI;
class ListScreen : public Screen {
public:
ListScreen();
~ListScreen();
void keyPressEvent(int keyCode, int nativeCode) {}
void keyReleaseEvent(int keyCode, int nativeCode) {}
void pointerPressEvent(MAPoint2d point) {}
void pointerReleaseEvent(MAPoint2d point) {}
void pointerMoveEvent(MAPoint2d point) {}
void show();
};
}
}
#endif //_LISTSCREEN_H_
ListScreen.cpp
#include "MAUI/Screen.h"
#include "ListScreen.h"
using namespace MAUI;
using namespace CoolPlaces::Views;
void ListScreen::show() {
Screen::show();
};
I'm getting this error: D:\MosyncProjects\Views\ListScreen.cpp:22: Error: Unresolved symbol '__ZN4MAUI6Screen4showEv' line 22 in this Screen::show(); call (for purpose of this topic I removed some code). So what exactly am I doing wrong here?
You're including the header file, which tells that the function Screen::show() exists, but probably not linking the library, which has the implementation.
See this page: http://www.mosync.com/docs/sdk/cpp/guides/libs/working-with-mosync-libraries/index.html
Specifically:
As well as referencing the header files in your application code, you also need to specify the actual libraries that you want to use in the project's Build Settings (Project > Properties > MoSync Project > Build Settings):
It looks like maui.lib should contain the screen code.

#include don't really include the code in C++

I have a class defined as follow:
Mirf.h
class Mirf{
public:
void init(void);
};
Mirf.cpp
#include <Mirf.h>
Mirf mirf = Mirf();
Mirf::init(){
[Some Code Here]
}
The class Wireless uses the Mirf instance:
Wireless.h
#include <Mirf.h>
class Wireless {
public:
void init(void);
};
Wireless.cpp
#include <Wireless.h>
#include <Mirf.h>
Wireless::init(){
mirf.init();
}
My main method:
Main.cpp
#include <Wireless.h>
#include <Mirf.h>
Wireless wireless = Wireless();
int main(){
wireless.init();
}
The code compiles normally but the [Some Code Here] in Mirf.cpp doesn't called:
What's is wrong in my code?
Just because code is #included doesn't mean that it will be called. Only the main function is called automatically, any other functions need to be referenced inside expressions or they won't run.