code::blocks smart sense / autocomplection not working with namespaces - c++

I'm trying to figure out why I cannot see any hints when calling object constructor. The weird thing is that I don't see it when I'm not using "using namespace ke;" (my namespace), but when I use it everything is fine. This error only affects constructors. I'm using Code::Blocks 17.12
Not working:
//includes
int main()
{
ke::Circle circle_test(/*arguments*/); // no arguments hints
circle_test.setPosition(69, 420); // works fine
// the rest of the code
}
Everything is fine:
//includes
using namespace ke;
int main()
{
Circle circle_test(/*arguments*/);
circle_test.setPosition(69, 420); // works fine too
// the rest of the code
}
Is this because I'm using old code blocks version or I added my files recursively? Or maybe something with constructor declaration?
EDIT
includes:
#include "KEngine/Graphics.hpp"
#include <SFML/Graphics.hpp>
#include <cmath>
KEngine Graphics includes ke::Circle
#include "Objects/Circle.hpp"

Related

How to set TextBlock properties programmatically using C++/WinRT + WinUI 3

I've been hiding under the MFC rock for many years so I can stick to standard C++ but still write Windows Desktop apps. With C++/WinRT and WinUI 3.0, it appears that I may finally have an opportunity to modernize my code. The problem is that I know nothing about XAML or the Windows API. To fix this problem, I'm trying to work my way through Petzold's "Programming Windows, 6th ed.", replacing the C# code with C++/WinRT. When all I have to do is write XAML, all is copacetic. However, when I get to p. 24, I'm supposed to adjust TextBlock properties in code. Here's the C#:
TextBlock tb = new TextBlock();
tb.Text = "Hello, Windows 8!";
tb.FontFamily = new FontFamily("Times New Roman");
tb.FontSize = 96;
tb.FontStyle = FontStyle.Italic;
...
and here's my attempt at a replacement:
TextBlock tb;
tb.Text(L"Hello, Windows 8!");
tb.FontFamily(FontFamily(L"Times New Roman"));
tb.FontSize(96);
tb.FontStyle(FontStyle::Italic);
...
All goes well until the last line. "FontStyle::Italic" is not recognized. I have similar issues with the enums for Color and HorizontalAlignment. What is the correct way to access these enums? Have I forgotten an include or a "using"? Here's what I currently have:
#include <winrt/Windows.Foundation.h>
#include <winrt/Windows.Foundation.Collections.h>
#include <winrt/Windows.ApplicationModel.Activation.h>
#include <winrt/Microsoft.UI.Composition.h>
#include <winrt/Microsoft.UI.Text.h>
#include <winrt/Microsoft.UI.Xaml.h>
#include <winrt/Microsoft.UI.Xaml.Controls.h>
#include <winrt/Microsoft.UI.Xaml.Controls.Primitives.h>
#include <winrt/Microsoft.UI.Xaml.Data.h>
#include <winrt/Microsoft.UI.Xaml.Interop.h>
#include <winrt/Microsoft.UI.Xaml.Markup.h>
#include <winrt/Microsoft.UI.Xaml.Media.h>
#include <winrt/Microsoft.UI.Xaml.Navigation.h>
#include <winrt/Microsoft.UI.Xaml.Shapes.h>
and
using namespace winrt;
using namespace Microsoft::UI::Text;
using namespace Microsoft::UI::Xaml::Controls;
using namespace Microsoft::UI::Xaml::Controls::Primitives;
using namespace Microsoft::UI::Xaml::Media;
I hope there's a short answer to my long question.
To be sure, you'd need to post a little more detail (like what error message you're getting). But I'll take a guess that perhaps your code is missing a namespace qualifier. I'm just going off the UWP Windows namespace types, not the WinUI Microsoft namespace, but this code builds for me:
#include <winrt/Windows.UI.h>
#include <winrt/Windows.UI.Text.h>
#include <winrt/Windows.UI.Xaml.h>
#include <winrt/Windows.UI.Xaml.Controls.h>
#include <winrt/Windows.UI.Xaml.Media.h>
void f()
{
winrt::Windows::UI::Xaml::Controls::TextBlock tb;
tb.Text(L"Hello");
tb.FontFamily(winrt::Windows::UI::Xaml::Media::FontFamily(L"Times New Roman"));
tb.FontSize(96);
tb.FontStyle(winrt::Windows::UI::Text::FontStyle::Italic);
tb.SelectionHighlightColor(winrt::Windows::UI::Xaml::Media::SolidColorBrush(winrt::Windows::UI::Colors::Red()));
tb.HorizontalAlignment(winrt::Windows::UI::Xaml::HorizontalAlignment::Center);
}
As does this:
#include <winrt/Windows.UI.h>
#include <winrt/Windows.UI.Text.h>
#include <winrt/Windows.UI.Xaml.h>
#include <winrt/Windows.UI.Xaml.Controls.h>
#include <winrt/Windows.UI.Xaml.Media.h>
using namespace winrt;
using namespace winrt::Windows::UI;
using namespace winrt::Windows::UI::Text;
using namespace winrt::Windows::UI::Xaml;
using namespace winrt::Windows::UI::Xaml::Controls;
using namespace winrt::Windows::UI::Xaml::Media;
void f()
{
TextBlock tb;
tb.Text(L"Hello");
tb.FontFamily(FontFamily(L"Times New Roman"));
tb.FontSize(96);
tb.FontStyle(FontStyle::Italic);
tb.SelectionHighlightColor(SolidColorBrush(Colors::Red()));
tb.HorizontalAlignment(HorizontalAlignment::Center);
}

Application crash when using library from the program compiled with make

I'm making scientific calculator in command line in c++ for my usage and also for practice. I have a problem with compiling it using cmake with mingw on windows. These are my source files:
main.ccp
#include <iostream>
#include <string>
#include "ExpressionCalculations/ExpressionParser.h"
int main()
{
std::string humanReadableExpression;
std::cout<<"Enter expression\n";
std::getline(std::cin, humanReadableExpression);
std::cout<<humanReadableExpression;
ExpressionCalculations::ExpressionParser parser;
auto&& expression = parser.GenerateRpnExpression(humanReadableExpression);
return 0;
}
ExpressionParser.h
#pragma once
#include <memory>
#include <stack>
#include <string>
#include <unordered_map>
namespace ExpressionCalculations
{
class ExpressionParser
{
public:
std::unique_ptr<std::string> GenerateRpnExpression(std::string &humanReadableExpression);
private:
// other code
};
}
ExpressionParser.cpp
#include <memory>
#include <stack>
#include <string>
#include <unordered_map>
#include <iostream>
#include "ExpressionParser.h"
namespace ExpressionCalculations
{
std::unique_ptr<std::string> ExpressionParser::GenerateRpnExpression(
std::string& humanReadableExpression)
{
std::unique_ptr<std::string> rpnExpression;
*rpnExpression="3456";
return rpnExpression;
}
These are cmake files
main CMakeLists.txt
cmake_minimum_required (VERSION 2.8)
project (ScientificCalculator_exe)
add_subdirectory(ExpressionCalculations)
add_executable(ScientificCalculator main.cpp)
target_link_libraries(ScientificCalculator ExpressionCalculations)
module CMakeList.txt
set(calculators ExpressionParser.h ExpressionParser.cpp)
add_library(ExpressionCalculations ${calculators})
When I run it , I can see Enter expression and pass input. Then I get Segmentation fault. However when I remove declaration of ExpressionParser and auto&& expression the string is shown, a string can be inputted and shown in the command. I checked configuration question multiple directories under cmake, https://cmake.org/cmake-tutorial/ and https://www.codeproject.com/Articles/1181455/A-CMake-tutorial-for-Visual-Cplusplus-developers but it seems that I correctly made cmake files. I have no idea why it doesn't work. I use the latest mingw64 on windows with default make compilation parameters.
From the cppreference page on unique_ptr:
The class satisfies the requirements of MoveConstructible and MoveAssignable, but not the requirements of either CopyConstructible or CopyAssignable.
In your ExpressionParser::GenerateRpnExpression function you are attempting to copy the unique_ptr out of the function when you should be moving it. Try return std::move(rpnExpression)
After debugging program compiled with just g++ I found the problem. It was misunderstanding of unique_ptr' default constructor behaviour. I thought it would initialize std::string but after reading doc and checking it it does not initialize object and generates nullptr. Then I looked into Scott Myers's Modern Effective C++ how to initialize the unique_ptr. Instead of std::unique_ptr<std::string> rpnExpression; I used auto rpnExpression = std::make_unique<std::string>();. It works like charm. I checked compiling through cmake and there were not any problems.

Cannot get C++ code running

Here is the simple code. I cant figure out why the code would compile but not run. I tried CodeBlocks and DevC++. non worked.
#include <iostream>
using namespace std;
int main()
{
cout<<"Hello";
return 0;
}
#include <iostream.h>
#include <windows.h>
int main()
{
cout<<"hi";
system("pause");//to hold console window
return EXIT_SUCCESS;
}
The code is absolutely correct and it compiles too. Make sure you are saving the file with .cpp extension. DevC++ works perfectly fine, there is no issue with the compiler.

Global namespace is not checked in other namespaces

I'm trying to use namespaces in my code so I have a header file that looks like this :
#include <string>
namespace AppNamespace
{
class A
{
std::string name;
};
}
When I try to compile this, it says "'string' is not a member of AppNamespace::std". If I remove the std:: in front of string, or if I write ::std::string name, then it will compile fine.
This is of course a simplified example, I have many header files and not all of them show this behavior. I am not sure what can cause this, I thought that the compiler would always try the global namespace as well.
I am currently using Visual Studio 2012 if this matters.
This is of course a simplified example, I have many header files and not all of them show this behavior. I am not sure what can cause this, I thought that the compiler would always try the global namespace as well.
At some point you must have something like this:
namespace AppNamespace
{
#include <string> // or #include "my_header" which in turn includes <string>
class A
{
std::string name;
};
}
The #include directive does not respect namespaces. You need to move them all out to the global namespace scope, or each (possibly nested) inclusion of a standard header will cause undefined behavior in the form of creating a nested namespace std.
Using:
using namespace std;
#include <iostream>
#include "test_header.h"
int main() ...
The code compiles either way with your above example as a header.
Moving
using namespace std;
below the header file (in my case test_header.h) will cause it to fail if I don't use std::string.
Is that the problem you are having?

Undefined reference to 'Class::Class'

After fixing the previous problem (see my one other question that I have asked). I had declared more classes.
One of these is called CombatAdmin which does various things: (Header file)
#ifndef COMBATADMIN_H
#define COMBATADMIN_H
#include <string> // Need this line or it complains
#include <Player.h>
#include <Sound.h>
#include <Enemy.h>
#include <Narrator.h>
using namespace std;
class Enemy;
class Player;
class CombatAdmin // Code yet to be commented here, will come soon.
{
public:
CombatAdmin();
void healthSet(double newHealth, string playerName);
void comAdSay(string sayWhat);
void playerFindsChest(Player *player,Weapon *weapon,Armour *armour);
void youStoleOurStuffEncounter(Player *player);
void comAdWarning(string enemyName);
void comAdAtkNote(string attack, double damage,string target,string aggresor);
void entDefeated(string entName);
void comAdStateEntHp(string ent, double hp);
void comAdStateScanResults(string enemyName, double enemyHealth);
string doubleToString(double number);
string intToString(int number);
bool isRandEncounter();
void randomEncounter(Player *player,Sound *sound,Narrator *narrator);
bool combatRound(Player *player, Enemy *enemy, Sound *sound, bool ran);
void playerFindsItem(string playerName,string itemName,double itemWeight,double playerWeight);
void playerFindsGold(string playerName,double coinCnt,double playerCoinCnt);
};
#endif // COMBATADMIN_H
It is then instanced in the main.cpp file like this: (Snippet of the main.cpp file)
#include <iostream> // Required for input and output
#include <Item.h> // Item header file.
#include <Weapon.h> // Header files that I have made for my classes are needed for this program
#include <sstream> // Needed for proper type conversion functions
#include <windows.h> // for PlaySound() and other functions like sleep.
#include <time.h> // Needed to seed the rand() function.
#include <mmsystem.h> // Not sure about this one, possibly defunct in this program.
#include <stdio.h> // Needed for a similar kind of output as iostream for various functions error msgs.
#include <irrKlang.h> // The header file of the sound lib I am using in this program.
#include <Narrator.h> // The narrators's header file.
#include <Pibot.h> // Other header files of classes.
#include <Armour.h>
#include <Player.h>
#include <Weapon.h>
#include <CombatAdmin.h>
using namespace irrklang;
using namespace std;
// Forward referenced functions
void seedRandom(); // Seeds the random number so it will be random as apposed to pseudo random.
string getPlayerName(string temp); // Gets the player's new name.
int main(int argc, char* argv[])
{
// Variables and object pointers declared here.
CombatAdmin *comAd = new CombatAdmin(); // Handles combat.
Narrator *narrator = new Narrator(); // The Narrator that says stuff
Pibot *piebot = new Pibot(); // PIbot, the player's trusty companion
string temp; // Temp string for input and output
However, when I try to compile the project, I get the following error:
C:\Documents and Settings\James Moran.HOME-B288D626D8\My Documents\C++ projects\Test Project\main.cpp|59|undefined reference to `CombatAdmin::CombatAdmin()'|
I am using the Code::Blocks IDE (ver 10.05), with the GNU GCC compiler. The project is of type "Console application". I am using windows XP 32 bit SP3.
I have tried changing to search directories to include where the object files are, but no success there.
As can be seen from the code, the narrator and PIbot are instanced just fine. (then used, not shown)
My question is, therefore, what do I need to do to stop these errors occurring? As when I encountered similar "Undefined reference to x" errors before using libraries. I had just forgotten to link to them in Code::Blocks and as soon as I did, they would work.
As this class is of my own making I am not quite sure about this.
Do say if you need more information regarding the code etc.
You have declared the default constructor (CombatAdmin()) and thus prevented the compiler from automatically generating it. Thus, you either need to 1) remove declaration of the default constructor from the class, or 2) provide an implementation.
I had this kind of error and the cause was that the CombatAdmin.cpp file wasn't selected as a Build target file: Prject->Properties->Build targets
Are you sure you've to include your header as:
#include <CombatAdmin.h>
?
I think you need to include your header file as:
#include "CombatAdmin.h"
And same for other headers written by you, like these:
#include "Armour.h"
#include "Player.h"
#include "Weapon.h"
//and similarly other header files written by you!
See this topic:
What is the difference between #include <filename> and #include "filename"?
My solution was just to add a line in the header before the class defenition:
class CombatAdmin;