标签: virtual

未定义方法的虚拟属性问题

我过去曾使用虚拟属性,但我似乎无法超越这个,而且我知道答案可能正在盯着我.

我有一个这样的模型:

model Confirmation.rb

class Confirmation < ActiveRecord::Base

  #attr_accessible :confirmation, :confirmation_token
  #attr_accessible :confirmation_token

  def confirmation_token
    confirmation.confirmation_token if confirmation
  end

  def confirmation_token=(token)
    self.confirmation = Booking.find_by_confirmation_token(token)
  end

end
Run Code Online (Sandbox Code Playgroud)

你的平均脚手架控制器

confirmations_controller.rb

  def new
    @confirmation = Confirmation.new(:confirmation_token => params[:confirmation_token])

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @confirmation }
    end
  end
Run Code Online (Sandbox Code Playgroud)

new.html.erb

<h1>New confirmation</h1>

<% form_for(@confirmation) do |f| %>
  <%= f.error_messages %>

    <%= f.hidden_field :confirmation_token %>

...
Run Code Online (Sandbox Code Playgroud)

的routes.rb

  map.confirmation "confirmation/:confirmation_token", :controller => "confirmations", :action => "new"
  map.resources :confirmations
Run Code Online (Sandbox Code Playgroud)

错误

未定义的方法`confirm ='for# …

ruby virtual attributes ruby-on-rails

0
推荐指数
1
解决办法
2389
查看次数

使用抽象类来实现派生类的元素堆栈

我必须在我的大学进行基础C++讲座,所以要明确一点:如果我被允许,我会使用STL.

问题:我有一个名为"shape3d"的类,我从中派生出类"cube"和"sphere".现在我必须实现"shape3d_stack",这意味着能够保存"cube"和"sphere"类型的对象.我使用了数组,当我尝试使用一堆int时,它运行得很好.我试着这样做:

shape3d_stack.cpp:

15    // more stuff
16    
17        shape3d_stack::shape3d_stack (unsigned size) :
18         array_ (NULL),
19         count_ (0),
20         size_  (size)
21        { array_ = new shape3d[size]; }
22    
23    // more stuff
Run Code Online (Sandbox Code Playgroud)

但是,不幸的是,编译器告诉我:

g++ -Wall -O2 -pedantic -I../../UnitTest++/src/ -c shape3d_stack.cpp -o shape3d_stack.o
shape3d_stack.cpp: In constructor ‘shape3d_stack::shape3d_stack(unsigned int)’:
shape3d_stack.cpp:21: error: cannot allocate an object of abstract type ‘shape3d’
shape3d.hpp:10: note:   because the following virtual functions are pure within ‘shape3d’:
shape3d.hpp:16: note:  virtual double shape3d::area() const
shape3d.hpp:17: note:  virtual double …
Run Code Online (Sandbox Code Playgroud)

c++ virtual stack abstract-class derived-class

0
推荐指数
1
解决办法
594
查看次数

我应该使用物理页面列表吗?

我有一张表有很多记录.现在我在一页上选择它们.但我想创建一个列表.该列表将包含多个(约5-10)页.

为此,我应该使用物理(page1.php | page2.php | page3.php)还是虚拟页面(page.php?number = 1 | page.php?number = 2 | page.php?number3)?

php virtual list

0
推荐指数
1
解决办法
113
查看次数

对于直到运行时才创建的方法的虚方法?

我在理解C++中虚方法的目的时遇到了一些麻烦.如果方法不是在编译时创建的,那么方法是否必须是虚拟的?例如,如果你必须在运行时选择一个农场动物,那么所有动物的方法都需要是虚拟的,因为在用户选择一个之前你不知道它是否会被创建.如果我错了,请纠正我.

c++ methods virtual runtime

0
推荐指数
1
解决办法
131
查看次数

C++虚函数无法识别

我正在使用CodeBlocks,我有以下代码,无法编译.

(这是关于一些C++陷阱所以我唯一要问的是为什么它不能编译)

代码如下:

#include <iostream>
using namespace std;

class Shape
{
        public:
                Shape();
                virtual void reset();
        private:
                int color;
};

class Point : public Shape
{
        private:
        double a,b;
};

void Shape::reset()
{
        cout<<"Shape reset\n";
}

void Point::reset()
{
        Shape::reset();
        cout<<"Point reset";
}

Shape::Shape()
{
        reset();
}

int main()
{
        Shape s;
        Point o;
}
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

no `void Point::reset()' member function declared in class `Point'
Run Code Online (Sandbox Code Playgroud)

c++ virtual overriding class

0
推荐指数
1
解决办法
226
查看次数

为什么子类调用其父方法而不是它自己的方法?

我自己学习c ++并且遇到了一个我没想到的行为.我不确定,但确实认为这不是Java在类似情况下会做的事情.为什么是DogCat依靠TE父类的实现,使一个普通的声音?

#include <iostream>

class Animal{
    public:
        virtual void speak(){std::cout << "???" << std::endl; /* not implemented */}
};

class Dog : public Animal{
    public:
        virtual void speak(){
            std::cout << "Woof" << std::endl;
        }
};

class Cat : public Animal{
    public:
        virtual void speak(){
            std::cout << "Meow" << std::endl;
        }
};

class Desk{
    /* Not an animal */
};

template<class T>
void please_talk(){
    T anim;
    Animal ani = anim;
    anim.speak();
    ani.speak();
}

int main()
{
    please_talk<Dog>(); …
Run Code Online (Sandbox Code Playgroud)

c++ methods virtual inheritance class

0
推荐指数
1
解决办法
91
查看次数

抽象类重载ostream运算符

我有一个基本的抽象类Base.

class Base
{
protected:
    string m_Name;
public:
    virtual string Name() { return m_Name; }
    virtual string Type() = 0;
    virtual bool isEqual(Base* rhs) = 0 ;
    //virtual ostream& operator<< (ostream& out) const;
};
Run Code Online (Sandbox Code Playgroud)

我想重载operator <<显示继承的对象Base.我不能使用void print()函数,因为这些继承的对象Base也有一些只能显示的对象operator <<.

我怎么能超负荷operator <<

c++ virtual overloading abstract ostream

0
推荐指数
1
解决办法
640
查看次数

为什么在类声明之外不允许使用虚拟和静态关键字?

我很想知道为什么C++中不允许使用以下内容?

第一个项目:

#include <iostream>
class Test {
    public:
        int myfun();
}
virtual int Test::myfun()
{ return 0; }
int main()
{ }
Run Code Online (Sandbox Code Playgroud)

[错误]'虚拟'外部类声明

第二个项目:

#include <iostream>
class Test {
    public:
        int myfun();
};
static int myfun() {
    std::cout<<"This program contains an error\n";
    return 0; 
}
int main() {
  Test::myfun(); 
  return 0; 
}
Run Code Online (Sandbox Code Playgroud)

[Error]无法在没有对象的情况下调用成员函数'int Test :: myfun()'

所以,我的问题是

为什么我不能像第一个程序那样使成员函数虚拟化?

为什么我不能像第二个程序那样使成员函数静态化?

有没有理由在课外不允许这两个关键词?

c++ syntax virtual static

0
推荐指数
1
解决办法
4469
查看次数

C++销毁基类导致调试断言失败

我正在尝试学习更多关于多态的知识,我试图让它变得尽可能简单,所以我得到了基类图和2个派生类Rectangle和Circle.

class Figure {
public:
    Figure() { cout << "Creating Figure\n"; }
    virtual ~Figure() { cout << "Destroying Figure\n"; }

    virtual double calculateField() {
            return 0;
    }
};

class Rectangle : public Figure
{
public:
    Rectangle(double m_a) : p_a(m_a) { cout << "Creating Rectangle\n"; }
    ~Rectangle() { cout << "Destroying Rectangle\n"; }

    virtual double calculateField() {
        return p_a*p_a;
    }

private:
    double p_a; 
};

class Circle : public Figure
{
public:
    Circle(double m_r) : p_r(m_r) { cout << "Creating Circle\n"; } …
Run Code Online (Sandbox Code Playgroud)

c++ polymorphism virtual

0
推荐指数
1
解决办法
75
查看次数

继承覆盖

我有一个关于继承覆盖期间早/晚绑定的问题.

所以我将介绍基础知识OOP for C++,并读取如果你没有在基类virtual上声明一个函数,你就不能覆盖它.但是我有以下代码,看起来我的编译器无论如何都会覆盖它.

#include <iostream>

using namespace std;

class Book{
public:
    string title;
    int number;

    Book(){
    }

    Book(string title){
        cout << "book " << title << " created" << endl;
    }

    void setNumber(int num){
        number = num + 7;
    }

    ~Book(){
        cout << "book " << title << " destroyed:";
    }
};

class Magazine: public Book {
public:

    void setNumber(int num){
    number = num;
    }
};

int main()
{
    Magazine mag;
    mag.setNumber(4);
    cout << mag.title << endl;
    cout << …
Run Code Online (Sandbox Code Playgroud)

c++ oop virtual

0
推荐指数
1
解决办法
121
查看次数