2013-04-06 86 views
0

我想純虛函數。所以我寫了相應的代碼。 但我可能沒有得到任何問題,因爲這一點。我在代碼中得到「cout沒有命名類型」錯誤,即使我也包含了適當的頭文件和命名空間。 請給出你的建議。「COUT」沒有指定類型的錯誤

#include<iostream> 
using namespace std; 

struct S { 
    virtual void foo(); 
}; 

void S::foo() { 
    // body for the pure virtual function `S::foo` 
cout<<"I am S::foo()" <<endl; 
} 

struct D : S { 
    cout <<"I am inside D::foo()" <<endl; 
}; 

int main() { 
    S *ptr; 
    D d; 
    ptr=&d; 
    //ptr->foo(); 
    d.S::foo(); // another static call to `S::foo` 
    cout <<"Inside main().." <<endl; 
    return 0; 
} 

回答

4

您試圖定義一個結構直接的代碼,但它看起來像你想圍繞代碼方法

struct D : S { 
    cout <<"I am inside D::foo()" <<endl; 
}; 

也許應該

struct D : S { 
    virtual void foo() { 
    cout <<"I am inside D::foo()" <<endl; 
    } 
};