2016-02-26 2 views
1

У меня проблема с C++/CX. Я пытаюсь создать класс, который на самом деле является сборником другого класса. не класс объявляется здесь в заголовочном файле:C++/CX Нет подходящего конструктора копирования

#pragma once 
namespace AdeptlyAdaptiveLayout 
{ 

    public ref class NewsItem sealed 
    { 
    public: 
    NewsItem(int init_Id, Platform::String^ init_Category, Platform::String^ init_Headline, Platform::String^ init_Subhead, Platform::String^ init_DateLine, Platform::String^ Image); 
    property int Id; 
    property Platform::String^ Category; 
    property Platform::String^ Headline; 
    property Platform::String^ Subhead; 
    property Platform::String^ DateLine; 
    property Platform::String^ Image; 
    }; 

    public ref class NewsItemCollection sealed 
    { 
    public: 
    Platform::Collections::Vector<NewsItem> getNewsItems(); 
    }; 
} 

и это исходный файл

#include "pch.h" 
#include "NewsItem.h" 

using namespace Platform; 
using namespace Platform::Collections; 

namespace AdeptlyAdaptiveLayout 
{ 
    NewsItem::NewsItem(int init_Id, 
     String^ init_Category, 
     String^ init_Headline, 
     String^ init_Subhead, 
     String^ init_DateLine, 
     String^ init_Image) 
    { 
     Id = init_Id; 
     Category = init_Category; 
     Headline = init_Headline; 
     Subhead = init_Subhead; 
     DateLine = init_DateLine; 
     Image = init_Image; 
    } 

    Vector<NewsItem> NewsItemCollection::getNewsItems() 
    { 
     Vector<NewsItem> temp; 

     temp.Append(*ref new NewsItem(1, "Financial", "Lorem Ipsum", "doro sit amet", "Nunc tristique nec", "Assets/Financial1.png")); 
     temp.Append(*ref new NewsItem(2, "Financial", "Etiam ac felis viverra", "vulputate nisl ac, aliquet nisi", "tortor porttitor, eu fermentum ante congue", "Assets/Financial2.png")); 
     temp.Append(*ref new NewsItem(3, "Financial", "Integer sed turpis erat", "Sed quis hendrerit lorem, quis interdum dolor", "in viverra metus facilisis sed", "Assets/Financial3.png")); 
     temp.Append(*ref new NewsItem(4, "Financial", "Proin sem neque", "aliquet quis ipsum tincidunt", "Integer eleifend", "Assets/Financial4.png")); 
     temp.Append(*ref new NewsItem(5, "Financial", "Mauris bibendum non leo vitae tempor", "In nisl tortor, eleifend sed ipsum eget", "Curabitur dictum augue vitae elementum ultrices", "Assets/Financial5.png")); 

     temp.Append(*ref new NewsItem(6, "Food", "Lorem ipsum", "dolor sit amet", "Nunc tristique nec", "Assets/Food1.png")); 
     temp.Append(*ref new NewsItem(7, "Food", "Etiam ac felis viverra", "vulputate nisl ac, aliquet nisi", "tortor porttitor, eu fermentum ante congue", "Assets/Food2.png")); 
     temp.Append(*ref new NewsItem(8,"Food", "Integer sed turpis erat", "Sed quis hendrerit lorem, quis interdum dolor", "in viverra metus facilisis sed","Assets/Food3.png")); 
     temp.Append(*ref new NewsItem(9, "Food","Proin sem neque", "aliquet quis ipsum tincidunt", "Integer eleifend", "Assets/Food4.png")); 
     temp.Append(*ref new NewsItem(10, "Food", "Mauris bibendum non leo vitae tempor", "In nisl tortor, eleifend sed ipsum eget", "Curabitur dictum augue vitae elementum ultrices", "Assets/Food5.png")); 

     return temp; 
    } 

} 

Проблема заключается в том, что я держу имея это сообщение об ошибке «класс„AdeptlyAdaptiveLayout :: статьи новостей“не имеет подходящий конструктор копирования ". Я не знаю, что я сделал неправильно. Можете ли вы, ребята, понять?

+0

Почему использовать 'реф new', когда вы просто хотите объект, то почему бы не сделать 'temp.Append (NewsItem (...))' непосредственно? –

+0

Что касается вашей проблемы, я не знаю специфики C++/CX, но, похоже, компилятор не может создать неявный конструктор-копию для класса по умолчанию, вам нужно явно создать экземпляр-конструктор. При передаче объекта функции «Append» копия * копируется * для хранения в векторе, если компилятор не может создать эту копию, тогда вы получите сообщение об ошибке, подобное тому, которое у вас есть. –

ответ

0

Хорошо, я нашел ответ сейчас. Я изменил тип с Vector на IVector, явно создал конструктор копирования и добавил несколько изменений в реализацию. Вот измененный код в заголовке и в источнике.

Заголовок:

namespace AdeptlyAdaptiveLayout 
{ 

    public ref class NewsItem sealed 
    { 
    public: 
     NewsItem(int init_Id, Platform::String^ init_Category, Platform::String^ init_Headline, Platform::String^ init_Subhead, Platform::String^ init_DateLine, Platform::String^ Image); 
     NewsItem(NewsItem^ obj); 
     property int Id; 
     property Platform::String^ Category; 
     property Platform::String^ Headline; 
     property Platform::String^ Subhead; 
     property Platform::String^ DateLine; 
     property Platform::String^ Image; 
    }; 

    public ref class NewsItemCollection sealed 
    { 
    public: 
     Windows::Foundation::Collections::IVector<NewsItem^>^ getNewsItems(); 
    }; 
} 

Источник:

#include "pch.h" 
#include "NewsItem.h" 

using namespace Platform; 
using namespace Platform::Collections; 
using namespace Windows::Foundation::Collections; 

namespace AdeptlyAdaptiveLayout 
{ 
    NewsItem::NewsItem(int init_Id, 
     String^ init_Category, 
     String^ init_Headline, 
     String^ init_Subhead, 
     String^ init_DateLine, 
     String^ init_Image) 
    { 
     Id = init_Id; 
     Category = init_Category; 
     Headline = init_Headline; 
     Subhead = init_Subhead; 
     DateLine = init_DateLine; 
     Image = init_Image; 
    } 

    NewsItem::NewsItem(NewsItem^ obj) 
    { 
     this->Category = obj->Category; 
     this->DateLine = obj->DateLine; 
     this->Headline = obj->Headline; 
     this->Id = obj->Id; 
     this->Image = obj->Image; 
     this->Subhead = obj->Subhead; 
    } 

    IVector<NewsItem^>^ NewsItemCollection::getNewsItems() 
    { 
     IVector<NewsItem^>^ temp = ref new Vector<NewsItem^>(); 

     temp->Append(ref new NewsItem(1, "Financial", "Lorem Ipsum", "doro sit amet", "Nunc tristique nec", "Assets/Financial1.png")); 
     temp->Append(ref new NewsItem(2, "Financial", "Etiam ac felis viverra", "vulputate nisl ac, aliquet nisi", "tortor porttitor, eu fermentum ante congue", "Assets/Financial2.png")); 
     temp->Append(ref new NewsItem(3, "Financial", "Integer sed turpis erat", "Sed quis hendrerit lorem, quis interdum dolor", "in viverra metus facilisis sed", "Assets/Financial3.png")); 
     temp->Append(ref new NewsItem(4, "Financial", "Proin sem neque", "aliquet quis ipsum tincidunt", "Integer eleifend", "Assets/Financial4.png")); 
     temp->Append(ref new NewsItem(5, "Financial", "Mauris bibendum non leo vitae tempor", "In nisl tortor, eleifend sed ipsum eget", "Curabitur dictum augue vitae elementum ultrices", "Assets/Financial5.png")); 

     temp->Append(ref new NewsItem(6, "Food", "Lorem ipsum", "dolor sit amet", "Nunc tristique nec", "Assets/Food1.png")); 
     temp->Append(ref new NewsItem(7, "Food", "Etiam ac felis viverra", "vulputate nisl ac, aliquet nisi", "tortor porttitor, eu fermentum ante congue", "Assets/Food2.png")); 
     temp->Append(ref new NewsItem(8,"Food", "Integer sed turpis erat", "Sed quis hendrerit lorem, quis interdum dolor", "in viverra metus facilisis sed","Assets/Food3.png")); 
     temp->Append(ref new NewsItem(9, "Food","Proin sem neque", "aliquet quis ipsum tincidunt", "Integer eleifend", "Assets/Food4.png")); 
     temp->Append(ref new NewsItem(10, "Food", "Mauris bibendum non leo vitae tempor", "In nisl tortor, eleifend sed ipsum eget", "Curabitur dictum augue vitae elementum ultrices", "Assets/Food5.png")); 

     return temp; 
    } 

} 

Однако, я до сих пор не понимаю, почему это работает. Я думаю, что я собираюсь провести углубленное исследование этого.

0

При условии, что конструктор с этим конструктором подписи:

NewsItem(int init_Id, Platform::String .... 

... компилятор больше не будет синтезировать ctors по умолчанию для вас. Теперь вам нужно указать их сами в этом случае.

https://msdn.microsoft.com/en-us/library/s16xw1a8.aspx

Если какие-либо не по умолчанию конструкторы объявлены, компилятор не предоставлять конструктор по умолчанию

+0

Ссылка и правило применяются к классам C++ и C++. Это верно и для классов C++/CX ref? – IInspectable

+0

Да, это так. Поведение последовательное. Я считаю, что это верно и для GCC и GCC вариантов, но я не могу проверить его прямо сейчас ... У меня 8-недельный сон на моих коленях. – gdc

+0

Я не думаю, что GCC реализует C++/CX. У вас есть ссылка на это? – IInspectable

Смежные вопросы