使用桥接模式c ++实现复制构造函数(Implement copy constructor with bridge pattern c++)
我正在研究c ++并尝试实现桥接模式,当发生这种情况时,我的构建器有我的实现文件:
SystemImpl::SystemImpl() { this->name = ""; this->value = 0.0; this->maxValue = DBL_MAX; } SystemImpl::SystemImpl(const SystemImpl& sys) { this->name = sys.name; this->value = sys.value; this->maxValue = sys.maxValue; }
现在,我正在创建使用此实现的接口,其中imps是我指向实现类的指针:
System::System() { imps = new SystemImpl(); } System::System(const System& sys) { imps = new SystemImpl(sys); }
fisrt构造函数工作正常,但第二个,即复制构造函数, 没有显示调用'SystemImpl :: SystemImpl(const System&)'的匹配函数
怎么了?
I'm studying c++ and trying to implement the bridge pattern, when this happens, I have My implementation file with constructors:
SystemImpl::SystemImpl() { this->name = ""; this->value = 0.0; this->maxValue = DBL_MAX; } SystemImpl::SystemImpl(const SystemImpl& sys) { this->name = sys.name; this->value = sys.value; this->maxValue = sys.maxValue; }
And now, I'm creating the interface that use this implementation, where imps is my pointer to implementation class:
System::System() { imps = new SystemImpl(); } System::System(const System& sys) { imps = new SystemImpl(sys); }
The fisrt constructor work's fine, but the second, that's a copy constructor, shows no matching function for call to ‘SystemImpl::SystemImpl(const System&)’
What's wrong?
原文:https://stackoverflow.com/questions/35592725
最满意答案
这是从头开始在rails 4中使用carrierwave上载多个图像的解决方案
或者你可以找到工作演示: 多个附件Rails 4
要按照这些步骤做。
rails new multiple_image_upload_carrierwave
在宝石文件
gem 'carrierwave' bundle install rails generate uploader Avatar
创建脚手架
rails generate scaffold post title:string
创建post_attachment脚手架
rails generate scaffold post_attachment post_id:integer avatar:string rake db:migrate
在post.rb
class Post < ActiveRecord::Base has_many :post_attachments accepts_nested_attributes_for :post_attachments end
在post_attachment.rb
class PostAttachment < ActiveRecord::Base mount_uploader :avatar, AvatarUploader belongs_to :post end
在post_controller.rb
def show @post_attachments = @post.post_attachments.all end def new @post = Post.new @post_attachment = @post.post_attachments.build end def create @post = Post.new(post_params) respond_to do |format| if @post.save params[:post_attachments]['avatar'].each do |a| @post_attachment = @post.post_attachments.create!(:avatar => a) end format.html { redirect_to @post, notice: 'Post was successfully created.' } else format.html { render action: 'new' } end end end private def post_params params.require(:post).permit(:title, post_attachments_attributes: [:id, :post_id, :avatar]) end
在views / posts / _form.html.erb
<%= form_for(@post, :html => { :multipart => true }) do |f| %> <div class="field"> <%= f.label :title %><br> <%= f.text_field :title %> </div> <%= f.fields_for :post_attachments do |p| %> <div class="field"> <%= p.label :avatar %><br> <%= p.file_field :avatar, :multiple => true, name: "post_attachments[avatar][]" %> </div> <% end %> <div class="actions"> <%= f.submit %> </div> <% end %>
编辑任何帖子的附件和附件列表。 在views / posts / show.html.erb
<p id="notice"><%= notice %></p> <p> <strong>Title:</strong> <%= @post.title %> </p> <% @post_attachments.each do |p| %> <%= image_tag p.avatar_url %> <%= link_to "Edit Attachment", edit_post_attachment_path(p) %> <% end %> <%= link_to 'Edit', edit_post_path(@post) %> | <%= link_to 'Back', posts_path %>
更新表单以编辑附件视图/ post_attachments / _form.html.erb
<%= image_tag @post_attachment.avatar %> <%= form_for(@post_attachment) do |f| %> <div class="field"> <%= f.label :avatar %><br> <%= f.file_field :avatar %> </div> <div class="actions"> <%= f.submit %> </div> <% end %>
修改post_attachment_controller.rb中的更新方法
def update respond_to do |format| if @post_attachment.update(post_attachment_params) format.html { redirect_to @post_attachment.post, notice: 'Post attachment was successfully updated.' } end end end
在rails 3中,不需要定义强大的参数,并且可以在model和accept_nested_attribute中定义attribute_accessible来发布模型,因为在rails 4中不推荐使用属性。
为了编辑附件,我们不能一次修改所有的附件。 所以我们将一个接一个地替换附件,或者你可以根据你的规则进行修改。这里我只是向你展示如何更新任何附件。
This is solution to upload multiple images using carrierwave in rails 4 from scratch
Or you can find working demo : Multiple Attachment Rails 4
To do just follow these steps.
rails new multiple_image_upload_carrierwave
In gem file
gem 'carrierwave' bundle install rails generate uploader Avatar
Create post scaffold
rails generate scaffold post title:string
Create post_attachment scaffold
rails generate scaffold post_attachment post_id:integer avatar:string rake db:migrate
In post.rb
class Post < ActiveRecord::Base has_many :post_attachments accepts_nested_attributes_for :post_attachments end
In post_attachment.rb
class PostAttachment < ActiveRecord::Base mount_uploader :avatar, AvatarUploader belongs_to :post end
In post_controller.rb
def show @post_attachments = @post.post_attachments.all end def new @post = Post.new @post_attachment = @post.post_attachments.build end def create @post = Post.new(post_params) respond_to do |format| if @post.save params[:post_attachments]['avatar'].each do |a| @post_attachment = @post.post_attachments.create!(:avatar => a) end format.html { redirect_to @post, notice: 'Post was successfully created.' } else format.html { render action: 'new' } end end end private def post_params params.require(:post).permit(:title, post_attachments_attributes: [:id, :post_id, :avatar]) end
In views/posts/_form.html.erb
<%= form_for(@post, :html => { :multipart => true }) do |f| %> <div class="field"> <%= f.label :title %><br> <%= f.text_field :title %> </div> <%= f.fields_for :post_attachments do |p| %> <div class="field"> <%= p.label :avatar %><br> <%= p.file_field :avatar, :multiple => true, name: "post_attachments[avatar][]" %> </div> <% end %> <div class="actions"> <%= f.submit %> </div> <% end %>
To edit an attachment and list of attachment for any post. In views/posts/show.html.erb
<p id="notice"><%= notice %></p> <p> <strong>Title:</strong> <%= @post.title %> </p> <% @post_attachments.each do |p| %> <%= image_tag p.avatar_url %> <%= link_to "Edit Attachment", edit_post_attachment_path(p) %> <% end %> <%= link_to 'Edit', edit_post_path(@post) %> | <%= link_to 'Back', posts_path %>
Update form to edit an attachment views/post_attachments/_form.html.erb
<%= image_tag @post_attachment.avatar %> <%= form_for(@post_attachment) do |f| %> <div class="field"> <%= f.label :avatar %><br> <%= f.file_field :avatar %> </div> <div class="actions"> <%= f.submit %> </div> <% end %>
Modify update method in post_attachment_controller.rb
def update respond_to do |format| if @post_attachment.update(post_attachment_params) format.html { redirect_to @post_attachment.post, notice: 'Post attachment was successfully updated.' } end end end
In rails 3 no need to define strong parameters and as you can define attribute_accessible in both the model and accept_nested_attribute to post model because attribute accessible is deprecated in rails 4.
For edit an attachment we cant modify all the attachments at a time. so we will replace attachment one by one, or you can modify as per your rule, Here I just show you how to update any attachment.
相关问答
更多-
将图像从PhoneGap上传到Carrierwave / Rails 3?(Uploading image from PhoneGap to Carrierwave / Rails 3?)[2022-05-16]
您可以将图像作为Base64 / encode发送,然后在CarrierWave端解析它。 看看http://ograycoding.wordpress.com/2012/07/03/api-upload-with-carrierwave/ You can send the image as Base64/encode and than parse it on the CarrierWave side. Take a look at http://ograycoding.wordpress.com/2012 ... -
解决了Rails 5.1.4,Carrierwave 1.2.1 我没有attr_accesor :images 鉴于: = f.file_field :images, multiple: true = f.hidden_field :images_cache 在控制器中: def create @instance = Model.new(permited_parameters) add_images .......... @instance.save end private def ...
-
您曾经这样手动执行此操作: require 'carrierwave/processing/mime_types' class MyUploader < CarrierWave::Uploader::Base include CarrierWave::MimeTypes process :set_content_type end 现在你只需删除mime类型部分。 https://github.com/carrierwaveuploader/carrierwave#setting-the-con ...
-
这是从头开始在rails 4中使用carrierwave上载多个图像的解决方案 或者你可以找到工作演示: 多个附件Rails 4 要按照这些步骤做。 rails new multiple_image_upload_carrierwave 在宝石文件 gem 'carrierwave' bundle install rails generate uploader Avatar 创建脚手架 rails generate scaffold post title:string 创建post_attachme ...
-
查看CarrierWave::Workers::StoreAsset#perform源后,无法上传图像的原因是嵌入文档。 来自source record = resource.find id行将返回nil因为resource是嵌入类,并且您无法通过嵌入类找到id。 要解决这个问题,请看这里 。 我的代码应该工作,如果我添加以下内容: class Event include Mongoid::Document embeds_many :images end class EventImage i ...
-
尝试更改show.html.erb : 从:
Image: <%= @image.image %>
至:Image: <%= image_tag @image.image %>
try changing your show.html.erb: from:Image: <%= @image.image %>
to:...
-
问题不在于CarrierWare。 它限制了HTML和浏览器。 您需要使用javascript库来选择多个文件。 像这样https://blueimp.github.io/jQuery-File-Upload/或http://www.dropzonejs.com/ A problem is not of CarrierWare. It's restriction of HTML and browsers. You need to use a javascript library to select mult ...
-
使用Rails 4,Strong Parameters和Carrierwave进行文件上传(File upload with Rails 4, Strong Parameters and Carrierwave)[2022-11-03]
这与嵌套的属性一起工作: params.permit( ..., :photos_attributes => ['id', 'title', 'image', '_destroy']) 但看起来像Carrierwave或Nested Form应该先针对Rails 4进行更新。 它只是试图保存一个空的图像,我一直在消除:reject_if ,但它仍然不是100的工作。 This worked with the nested attributes: params.permit( ..., :photos_at ... -
如何使用cloudinary和carrierwave上传多个文件(How to upload multiple files with cloudinary and carrierwave)[2024-02-05]
根据image的值,您可以这样做: if params[:images] params[:images].each do |image| @post.pictures.create(image: image) end upload_images(@post.pictures) end def upload_images(images) images.each do |i| Cloudinary::Uploader.upload(i) end end Depending ... -
Rails 5 CarrierWave,无法删除多个上传中的最后一个文件(Rails 5 CarrierWave, can't remove last file in a multiple upload)[2023-10-13]
我遇到了同样的问题,我发现你必须打电话给'remove_images!' 如果这是最后一个。 在'remove_image_at_index'函数中添加: @gallery.remove_images! if remain_images.empty? 问候 I had the same problem and I found that you have to call 'remove_images!' if this was the last one. In 'remove_image_at_index' ...