lundi 28 janvier 2019

c++ template Incomplete type using pointer of class inside template

I am trying to create a grid, From what I understand the issue is coming from using a pointer of a template class inside its self which is legal until i try to do stuff with it is when the compiler complains. I am looking for a way to use pointer's to a class of a template inside its self for use in using the pointers and do what I will later. I compile with g++ version 5 the compile command i use is g++ *.cpp -o main -std=c++11 the error i get will follow the snippet for the code.

struct Vector2D 
{
    Vector2D(  ) {  }
    Vector2D( int x , int y ): x( x ) , y( y ) {  } ;

    int x , y ; 

} ;

template <typename A>
class GridNode2D ; 

template <typename T>
class GridNode2D
{
public: 
    GridNode2D(  ) {  } ;
    T data ; 
    Vector2D coOrdinate ; 

    GridNode2D<T>* left, right, up, down ; 

} ;

template <typename T>
class Grid2D
{
public:
    Grid2D(  ) ;

    GridNode2D<T>* head ; 

} ;

template <typename T>
Grid2D<T>::Grid2D(  )
{
    this->head = new GridNode2D<T> ; 
    this->head->right = new GridNode2D<T> ; 

} ;

Errors:

main.cpp: In instantiation of ‘class GridNode2D<bool>’:
<span class="error_line" onclick="ide.gotoLine('main.cpp',39)">main.cpp:39:16</span>:   required from ‘Grid2D<T>::Grid2D() [with T = bool]’
<span class="error_line" onclick="ide.gotoLine('main.cpp',47)">main.cpp:47:18</span>:   required from here
main.cpp:22:26: error: ‘GridNode2D::right’ has incomplete type
     GridNode2D<T>* left, right, up, down ; 
                          ^
main.cpp:15:7: note: definition of ‘class GridNode2D’ is not complete until the closing brace
 class GridNode2D
       ^
main.cpp:22:33: error: ‘GridNode2D::up’ has incomplete type
     GridNode2D<T>* left, right, up, down ; 
                                 ^
main.cpp:15:7: note: definition of ‘class GridNode2D’ is not complete until the closing brace
 class GridNode2D
       ^
main.cpp:22:37: error: ‘GridNode2D::down’ has incomplete type
     GridNode2D<T>* left, right, up, down ; 
                                     ^
main.cpp:15:7: note: definition of ‘class GridNode2D’ is not complete until the closing brace
 class GridNode2D

VS2010 with C++11 all feature support using QT creator

I would like to ask a question about VS2010 IDE environment with all the C++11 features. Currently we need to build our software where some libraries need C++11 all feature support but we are now still running VS2010 environment and currently we are not upgrading our VS2010 to VS2017 at this moment.

So Is there any way to build these library with alternative mentods like installing QT creator on Windows platform so that can use g++ 4.9.3 or later same as on linux ubuntu? I found some website mentioning about Cygwin installed on Windows but seems not clear how to do.

Thanks for your help!

Howto read out SharedMemory Mapped File as different datatypes on c++ side

@First of all I´m sorry if the code formatting is not working, it´s my first attempt...

I´m setting up a interprocess communication between a C# WPF application and a C++ DLL (which is used by a 3rd party application). The communication shall be used to communicate several different basic datatypes (bool, (u)integer, float, double, string). Therefor I planned to use the MemoryMappedFile as Field of unspecified Data with a size that fits my amount of data. I read in the configuration on both side using the same ConfigFile and then store my byte offsets from on to the other variable. e.g

//bool1 => memory[0]
//int1 => memory[1]
//float1 => memory[5]
//double1 => memory[9]
//int2 => memory[17] and so on...

But I´m not able to re-interpret the values on the c++ side without getting exceptions.

The SharedMemory communication itself works properly. I sent data from each side to the other. And If I fill the byte[] on the c# sid with 1 in each field, I can print out the whole buffer on the c++ side as characters and I see a SOH (ascii 0x01) for each field.

// read the MapViewOfFile ... works
auto buffer = (char*) MapViewOfFile(this->shm_handle, 
FILE_MAP_READ, 0, 0, this->shm_field_size);


//interpret as std::string and print out 
//makes a bunch of SOH SOH... in my logfile
std::string incomingPayload(buffer);
_HEILOG(incomingPayload);


//this line throws an exception (I´m not able to catch it)
int tmp_int = *reinterpret_cast<int*>(buffer, 0);
_HEILOG("field as int" << tmp_int);

// this block throws also an unknown exception...
void* v = (void*)buffer[0];int* ip = (int*)v;
int testi = *ip; 
_HEILOG("field as int2" << testi );

I expected both tries to work, as I expected to be able to navigate through the memory field by buffer[offset] (e.g buffer[0], buffer[1], buffer[5]... for above example).

And then do a reinterpretation of the upcoming bytes to read the data as bool, int, float or whatever. But my 3rd Party application throws unknown exceptions and my Logfile has no entries.

I´m thankfull for each hint.

Best Regards SU52

How to write a standard-like function that has higher overload priority than the std version

In a generic function I use the following idiom

template<class It1, class It2>
void do_something(It1 first, It1 second, It2 d_first){
    using std::copy;
    copy(first, second, d_first);
}

Now suppose I have several iterator in my namespace N.

namespace N{

  struct itA{using trait = void;};
  struct itB{using trait = void;};
  struct itC{using trait = void;};

}

An I want to overload copy for these iterators in this namespace. Naturally I would do:

namespace N{
    template<class SomeN1, class SomeN2>
    SomeN2 copy(SomeN1 first, SomeN1 last, SomeN2 d_first){
        std::cout << "here" << std::endl;
    }
}

However when I call do_something with N::A, N::B or N::C argument I get "ambiguous call to copy" even though these are in the same namespace as N::copy.

Is there a way to win over std::copy in the context of the original function above?

I though that if I put constrains over the template arguments then N::copy would be preferred.

namespace N{
    template<class SomeN1, class SomeN2, typename = typename SomeN1::trait>
    SomeN2 copy(SomeN1 first, SomeN1 last, SomeN2 d_first){
        std::cout << "here" << std::endl;
    }
}

but it doesn't help.

What other workarounds can I try for the generic call to copy to prefer to a copy in the namespace of arguments rather than std::copy.

Complete code:

#include<iostream>
#include<algorithm>
namespace N{
  struct A{};
  struct B{};
  struct C{};
}

namespace N{
    template<class SomeN1, class SomeN2>
    SomeN2 copy(SomeN1 first, SomeN1 last, SomeN2 d_first){
        std::cout << "here" << std::endl;
    }
}

template<class It1, class It2>
void do_something(It1 first, It1 second, It2 d_first){
    using std::copy;
    copy(first, second, d_first); // ambiguous call when It is from namespace N (both `std::copy` and `N::copy` could work.
}

int main(){
    N::A a1, a2, a3;
    do_something(a1, a2, a3); 
}

A typical error message is

error: call of overloaded ‘copy(N::A&, N::A&, N::A&)’ is ambiguous

Get GDI DC from ID3D11Texture2D for drawing

I have an implementation in directx9 where I have taken GDI DC to render drawing. But the similar code in directx11 does not get GDI DC instead throws invalid call exception.

Implementation in directx9:

IF_DX9ERR_THROW_HR(m_spIDevice->CreateTexture(UINT(cSizeOverlay.cx), UINT(cSizeOverlay.cy), 1, D3DUSAGE_DYNAMIC,  D3DFMT_X8R8G8B8, D3DPOOL_DEFAULT, &m_spIOverlay, nullptr));
m_spIOverlaySurface = nullptr;
IF_DX9ERR_THROW_HR(m_spIOverlay->GetSurfaceLevel(0, &m_spIOverlaySurface));
D3DSURFACE_DESC descOverlay;
::ZeroMemory(&descOverlay, sizeof(descOverlay));
IF_DX9ERR_THROW_HR(m_spIOverlaySurface->GetDesc(&descOverlay));
// fill the texture with the color key
CRect cRect(0, 0, descOverlay.Width, descOverlay.Height);
HDC hDC = nullptr;
IF_DX9ERR_THROW_HR(m_spIOverlaySurface->GetDC(&hDC));
::SetBkColor(hDC, colKey);
::ExtTextOut(hDC, 0, 0, ETO_OPAQUE, cRect, nullptr, 0, nullptr);
IF_DX9ERR_THROW_HR(m_spIOverlaySurface->ReleaseDC(hDC));

Implementation in directx11:

D3D11_TEXTURE2D_DESC desc;
ZeroMemory(&desc, sizeof(desc));
desc.Width = gsl::narrow_cast<UINT>(width);
desc.Height = gsl::narrow_cast<UINT>(height);
desc.ArraySize = 1;
desc.Format = DXGI_FORMAT_B8G8R8X8_UNORM;
desc.Usage = D3D11_USAGE_DYNAMIC;
desc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
desc.MipLevels = 1;
desc.SampleDesc.Count = 1;
desc.MiscFlags = D3D11_RESOURCE_MISC_GDI_COMPATIBLE;

ID3D11DevicePtr device = renderer->Device();
ID3D11Texture2DPtr  texture2D;
IF_FAILED_THROW_HR(device->CreateTexture2D(&desc, nullptr, &texture2D));    
// get texture surface
IDXGISurface1Ptr dxgiSurface1 = tex2D;

IF_FAILED_THROW_HR(dxgiSurface1->GetDC(FALSE, &m_overlayDC));
//Draw on the DC using GDI
if (!m_overlayDC) // we have lost the device
    THROW_PE(IDS_ERR_NO_VIDEO_HARDWARE);
::SetBkColor(m_overlayDC, m_effectConstants.m_keyColor);
::ExtTextOut(m_overlayDC, 0, 0, ETO_OPAQUE, overlayRect, nullptr, 0, nullptr);
//When finish drawing release the DC
dxgiSurface1->ReleaseDC(nullptr);

m_overlayDC = nullptr;

how to generatehash code of a string in c++ that will be limited range?

I have on my configuration file 100 values. Each value is build of 2 char that can be like 90 or AA or 04.

I want to generate hash code of each value - and store them in array that contain 100 element - and each of the values from the configuration will be save in the hash code index in the array.

The question:

How to create hash code from 2 char that the hash code is limited between 0 to 99

undefined reference to "***"

I have defined a class Cat which has a member function void Cat::miao() in Cat.h file. Then , I implement this function in Cat.cpp as the following code.

However, while compiling and linking, I got an error , say , "undefined reference to `Cat::miao()" .What wrong with the code?

My compiler is GNU c11.

-----Cat.h

#include<iostream>
#include<string>
using namespace std;
class Cat
{
    string name;
    public:
        Cat(const string&n):name(n){};
        void miao();
};

-----Cat.cpp

#include"Cat.h"
void Cat::miao()
{
    cout << name << endl;
}

-----main.cpp

#include"Cat.h"
int main()
{
    Cat tom("tom");
    tom.miao();
    return 1;
}

C:\Users****:K.o:main.cpp:(.text+0x69): undefined reference to `Cat:: miao()' collect2.exe: error: ld returned 1 exit status