tr1: boost vs vs2010, using shared_ptr without namespace - visual-studio-2010

trying to compile old project that has many uses of shared_ptr with the vs2010.
so, i have precompiled header (stdafx.h) with:
..
using namespace std;
..
#include "boost/shared_ptr"
using namespace boost;
and later in the code i intensively use shared_ptr spObject;
What should I change in the stdafx.h so that I will no need to replace everywhere in the code *shared_ptr* to *some_namespace::shared_ptr*?
Is it possible to do without namespace conflicts of boost/boost::tr1/std::tr1/std?
Now I have a lot of errors:
error C2872: 'shared_ptr' : ambiguous symbol
could be 'k:\boost10\boost\smart_ptr\shared_ptr.hpp(165) : boost::shared_ptr'
or 'c:\program files (x86)\microsoft visual studio 10.0\vc\include\memory(1418) : std::tr1::shared_ptr'

Don't put using namespace in headers, as you've discovered it can break headers that follow, and because you can't change those headers there isn't much you can do about it.
At function scope you can use a using declaration to disambiguate:
void f()
{
using std::tr1::shared_ptr;
shared_ptr<int> p;
}
But that won't work in the global namespace, because you've already polluted that scope with the careless using directives.

Related

C++/WinRT Error C2872 'IUnknown': ambiguous symbol when using namespace

C++, WinRT, VS2017, Windows10, Bluetooth LE
I have a stand-alone C++/WinRT VS2017 console app that was used for testing code to control a Bluetooth LE device. That console app works and I am now in the process of moving that code into an existing C++ MFC VS2017 app which also works.
In the existing MFC app I first installed the NuGet cppwinrt package that I used in the test console app (no problems)
I then put the various WinRt header files that were in the console app's pch.h into the MFC's stdafx.h file
#pragma comment(lib, "windowsapp")
#include <condition_variable>
#include "winrt\Windows.Foundation.h"
#include "winrt\Windows.Storage.Streams.h"
#include "winrt\Windows.Devices.Bluetooth.h"
#include "winrt\Windows.Devices.Bluetooth.Advertisement.h"
#include "winrt\Windows.Devices.Bluetooth.GenericAttributeProfile.h"
So far, so good. The MFC app still compiles without problems. However, I then put the "using namespace" entries, that were at the top of the console app's .cpp file, into the MFC's ...View.cpp and that is when I get the compile problems
using namespace winrt;
using namespace Windows::Foundation;
using namespace winrt::Windows::Foundation;
using namespace Windows::Storage::Streams;
using namespace Windows::Devices::Bluetooth;
using namespace Windows::Foundation::Collections;
using namespace Windows::Devices::Bluetooth::Advertisement;
using namespace Windows::Devices::Bluetooth::GenericAttributeProfile;
The compile error I get is:
Error C2872 'IUnknown': ambiguous symbol MyMFCApp c:\program files
(x86)\microsoft visual
studio\2017\professional\vc\tools\msvc\14.16.27023\atlmfc\include\atlcom.h 3456
I see that IUnknown seems to be fairly universal throughout Windows APIs. How should I go about clearing up this error?
#AlanBirtles answer seems to have solved the problem. In the console app I could not compile without these using namespace entries so I just assumed that I would also need them in the MFC app. As I mentioned in my comment above, all of the namespaces compiled in the MFC app except for the two Foundation namespaces.
//using namespace Windows::Foundation; // errors IUnknown
//using namespace winrt::Windows::Foundation; // errors IUnknown
I left the rest and commented these two out and then added the code to create the BluetoothLEAdvertisementWatcher, attach a callback for the watcher.Received() and let it find my device. It ran fine. I have not yet tried with the rest of the namespaces commented out but I will.
I did try #IInspectable suggestion for using the #include <unknwn.h> before all of the WinRT namespaces. I put that in at the top and then uncommented one of the two Foundation namespaces but still got the ambiguous IUnknown error.
So the answer seems to be (for my situation anyway) that the namespaces were not necessary...at least the two Foundation namespaces.
Thanks for the help. It is really appreciated.

Build for Mypintool sample for x64 using visual studio 2012 fails, if "windows.h" is included

I am trying to build The Mypintool sample that comes with pin distribution for x64 architecture.
I am using pin3.0 (build 76991) and Visual Studio 2012. The build is successful if I have not included windows.h.
But if I include window.h (in a separate namespace) like this:-
namespace WD {
#include "Windows.h"
}
Then the build gives the error :-
C:\Program Files (x86)\Windows Kits\8.0\Include\um\winnt.h(3486): error C2888: '_CONTEXT::<unnamed-tag>' : symbol cannot be defined within namespace 'WD'
C:\Program Files (x86)\Windows Kits\8.0\Include\um\winnt.h(3488): error C2888: '_CONTEXT::<unnamed-tag>::<unnamed-tag>' : symbol cannot be defined within namespace 'WD'
Also, I am able to build the tool for win32 with windows.h included without any issue. Also, I have compared the build settings for win32 and x64 and I could not find any discrepancy.
Any help is appreciated.
It is unclear to me if you have a windows application that makes use of "Pin" or a "Pin" application that needs to call some Windows APIs - or a hybrid where massive use of both APIs exists in a single program.
Nonetheless, the Windows SDK is quite large, and is designed (mostly) to work with C, or with a subset of C++ compatible with C so it cannot be expected to work if wrapped in a namespace.
So, your only effective way to deal with a conflict in headers, is to avoid it by never including the "pin" or "windows" headers in the same cpp file. You need to partition the parts of your program that call windows, and that call "pin" into separate cpp files.
Create a bridging header file that defines classes and functions that use only C++ declarations. As it makes no use of either pin, or windows, this file can be #included by both sides of your project. Of course, depending on what your application is attempting to achieve this may be difficult so you might have to engage in some heavy duty type erasure.
Something like this:
// pin.cpp
#include <intel/pin.h>
#include "bridge.h"
void method(){
Window* wnd = Window::Create();
wnd.Show();
}
.
// bridge.h
class Window {
public:
static Window* Create();
virtual void Show()=0;
};
.
// WindowsImpl.cpp
#include <windows.h>
#include "bridge.h"
class WindowImpl : public Window {
HWND hwnd;
public:
bool Create(){
hwnd = CreateWindowEx(...);
return hwnd != NULL;
}
void Show() override {
ShowWindow(hwnd,...);
}
};
Window* Window::Create(){
WindowImpl* newWnd = new WindowImpl();
newWnd->Create();
return newWnd;
}

C++/CX Header file can't find Microsoft namespace

I have a header file with the following code:
Microsoft::WRL:ComPtr<ID3D11Device2> m_device;
inside a class definition. Visual Studio 2013 is saying that Microsoft is not a namespace, if I take the code and cut it out and put it in another class in another file unchanged it works just fine!
Any ideas?
Philip
EDIT: All of a sudden (without me having changed anything) Intelissense now accepts Microsoft::WRL::ComPtry as valid but when I compile it still gives me errors that it does not exists.
You need to
#include <wrl.h>
or
#include <wrl/client.h>
To get Microsoft::WRL::ComPtr in your module.
When you say "Visual Studio 2013 is saying that Microsoft is not a namespace" do you mean you get a compiler error or is just Intellisense? When dealing with headers, Intellisense can get a bit out of sync until you build again. For example:
//Test.h
class A { Microsoft::WRL::ComPtr<T> a; };
//Test.cpp
#include <wrl/client.h>
#include "Test.h"
If you just added the #include <wrl/client.h> to the Test.cpp, Intellisense might not know yet it is in scope for the header. It's perfectly valid C++ already, but a better practice is to include in your headers the ones it needs like:
//Test.h
#pragma once
#include <wrl/client.h>
class A { Microsoft::WRL::ComPtr<T> a; };
The other way this sync issue can manifest itself is if you are doing:
//Test.h
class A { Microsoft::WRL::ComPtr<T> a; };
//Test.cpp
#include "pch.h"
#include "Test.h"
//pch.h
#include <wrl/client.h>
Again, fully valid C++ that will build. Intellisense knows it works when you build, but might not until then.
Note: WRL is traditional C++ and is not using C++/CX language extensions. They both exist to make it easier to consume WinRT APIs from C++, and you will see the Microsoft::WRL::ComPtr used inside C++/CX applications when dealing with non-WinRT COM APIs like Direct3D. And you can mix C++/CX with WRL in the same application taking advantage of the fact that you can use reinterpret_cast<> between C++/CX ref ^ and ABI COM pointers. You can use Microsoft::WRL::ComPtr in old-school Windows desktop apps on Windows 7 or Windows Vista too.
With all that said, WRL and C++/CX are two distinct things.
Update: For consuming Windows Runtime APIs, you can also use C++/WinRT which is also 'standard' C++ without any need for the C++/CX extensions. See Microsoft Docs. You can use Microsoft::WRL::ComPtr for C++/WinRT applications, or you can use their variant wrl::com_ptr

Unable to use "using namespace Microsoft::Win32" in visual studio 2010 c++ file,help me out

I'm u unable to use the following namespace "using namespace Microsoft::Win32"; in visual studio 2010.Actually I'm trying to access the system registry using this command.Whenever,I type this command i end up getting something like "name followed by :: must be a class or namespace name" .Help me out below mentioned is my code.
#include<stdio.h>
#include<conio.h>
#include<Windows.h>
#include <tchar.h>
#define MAX_KEY_LENGTH 255
#define MAX_VALUE_NAME 16383
using namespace Microsoft::Win32;
According to Microsoft examples on msdn.microsoft.com, there is such a namespace and their C++ examples use:
using namespace System;
using namespace System::Security::Permissions;
using namespace Microsoft::Win32;
You might also need to set your project to include the relevant Assembly, which is mscorlib (in mscorlib.dll).
Per comment by Creris below, apparently this also requires the CLR compiler option /clr.
(Oh, and sorry for posting the C# reference to Microsoft.Win32)

#using, #include, and 'assembly references' -- what are they and how do they relate?

I'm wondering how Visual Studio, other IDE's, and any other sort of circumstances (ie. no IDE at all) handle bringing in code from outside.
At first I thought #includes were the only way to do this, by either placing the assembly files in the designated directory for Visual Studio assembly files and then using the <> format to bring them in, or by putting the assembly files in the project directory and using the "" format to bring them in (that is, <> and "" respectively). But now I come up to the example at the end of this post with the #using directive (which, to note, is different than just the 'using' directive without the '#', for namespaces). Also I've come across adding assembly references in visual studio from within the 'configuration properties' dialogue.
So, would someone set me straight on all the in's and out's of adding assembly files and other code to a given project?
--The following is the example that has confused me-->
I have this section in my book that states:
"...The figure combines C++ 2008 code
with legacy C and native C++ code. It
also presents the two assembly
reference files you'll use most often
with C++ 2008, along with their
associated namespaces. Unlike when
you use Visual Studio to develop a
project, the assembly reference files
aren't included by default when you
code a single source file. Because of
that, you must code #using directives
for these files. ..."
#include <stdio.h>
#include <iostream>
#using <system.dll>
#using <system.windows.forms.dll>
// Associated namespace directives
using namespace std;
using namespace System;
using namespace System::Windows::Forms;
void main()
{
printf( "Hello, Earth\n"); // from stdio.h
cout << "Hello, Mars\n"; // from iostream
Console::WriteLine("Hello, Jupiter"); // from system.dll
MessageBox::Show ("Hello, Saturn"); // from system.windows.forms.dll
}
This is not native C++ (usually just referred to as C++), but C++/CLI, which is actually a .NET language designed to ease interacting between native and managed code, and as such can use both. It is, however, definitely not C++, despite an intentionally strong resemblance. Assemblies are .NET managed code repositories. You use the #using command to use them. #include is for native C++ headers. You should also be able to add managed references (that is, #using but done throughout for you) from the project's properties.
In native C++, then you must #include headers, and if appropriate, link to .lib files (or use GetProcAddress manually), and Visual Studio also offers #import for COM libraries. C++/CLI also offers #using for bringing in managed assemblies.
void main()
{
printf( "Hello, Earth\n"); // C native code
cout << "Hello, Mars\n"; // C++/CLI's wrapper on C++ Standard
Console::WriteLine("Hello, Jupiter"); // .NET managed code
MessageBox::Show ("Hello, Saturn"); // A thin wrapper on WinAPI
}
If you don't already know both C++ and .NET code, and/or you're not trying to link the two together, it's not recommended to use C++/CLI.

Resources