我遇到了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;
}
}
问题是我一直有这个错误消息&#34; class&#39; AdeptlyAdaptiveLayout :: NewsItem&#39;没有合适的拷贝构造函数&#34;。我不知道自己做错了什么。你能告诉我一个线索吗?
答案 0 :(得分:0)
好的,我现在找到了答案。我已经将类型从Vector更改为IVector,显式创建了复制构造函数,并在实现中添加了几个更改。以下是标题和源代码中已更改的代码。
部首:
<div class="pie_nut" id="pieChartContainer">
<canvas id="pieChart" height="5" width="6"></canvas>
</div>
$('#pieChartContainer').html(''); //remove canvas from container
$('#pieChartContainer').html('<canvas id="pieChart" height="5" width="6"></canvas>'); //add it back to the container
来源:
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();
};
}
但是,我仍然不明白为什么会这样。我想我会对此进行深入的研究。
答案 1 :(得分:0)
当您为构造函数提供此构造函数签名时:
NewsItem(int init_Id, Platform::String ....
...编译器将不再为您合成默认ctors。在这种情况下,您现在需要自己指定它们。
https://msdn.microsoft.com/en-us/library/s16xw1a8.aspx
如果声明了任何非默认构造函数,则编译器不会 提供默认构造函数