我正在嘗試將此 C 代碼轉換為 C。C 類需要轉換為結構。
此外,我們需要使用指標。
以下是 C 代碼:
#include <iostream>
class rectangle {
private:
double width,height;
public:
rectangle(double w, double h) {
width = w;
height = h;
}
double get_area() {
return width * height;
}
bool compare(rectangle* rf) {
return (this->get_area() > rf->get_area());
}
};
int main() {
rectangle* r1=new rectangle(1.5,2.6);
rectangle* r2=new rectangle(2.5,1.6);
bool ans=r1->compare(r2);
printf("%s\n", ans?"true":"false");
return 0;
}
我的 C 代碼不會引發任何錯誤,但不會列印任何內容。這是我的代碼。似乎找不到問題。你能幫忙嗎?
#include <stdio.h>
#include <stdbool.h>
struct Rectangle
{
double width;
double height;
void (*new_rectangle)(double, double);
double (*get_area)();
bool (*compare)(struct Rectangle*);
};
void new_rectangle(struct Rectangle* self, double w, double h)
{
self->width = w;
self->height = h;
}
double get_area(struct Rectangle* self)
{
return self->width*self->height;
}
bool compare(struct Rectangle* this, struct Rectangle* other) {
return (this->get_area() > other->get_area());
}
int main()
{
struct Rectangle* r1;
new_rectangle(r1, 1.5, 2.6);
struct Rectangle* r2;
new_rectangle(r2, 2.5, 1.6);
bool ans = r1->compare(r2);
printf("%s\n", ans?"true":"false");
// printf("%c", );
// bool ans=r1->compare(r2);
// printer(r1)
// printf("%s\n", ans?"true":"false");
return 0;
}
uj5u.com熱心網友回復:
您的 C 代碼未在 中設定函式指標struct,并且未在 中正確使用指標main()。
試試這個:
#include <stdio.h>
#include <stdbool.h>
struct Rectangle
{
double width;
double height;
double (*get_area)(struct Rectangle*);
bool (*compare)(struct Rectangle*);
};
double get_area(struct Rectangle* this)
{
return this->width * this->height;
}
bool compare(struct Rectangle* this, struct Rectangle* other) {
return this->get_area(this) > other->get_area(other);
}
void new_rectangle(struct Rectangle* this, double w, double h)
{
this->width = w;
this->height = h;
this->get_area = &get_area;
this->compare = &compare;
}
int main()
{
struct Rectangle r1;
new_rectangle(&r1, 1.5, 2.6);
struct Rectangle r2;
new_rectangle(&r2, 2.5, 1.6);
bool ans = r1.compare(&r1, &r2);
printf("%s\n", ans ? "true" : "false");
return 0;
}
或者:
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
struct Rectangle
{
double width;
double height;
double (*get_area)(struct Rectangle*);
bool (*compare)(struct Rectangle*);
};
double get_area(struct Rectangle* this)
{
return this->width * this->height;
}
bool compare(struct Rectangle* this, struct Rectangle* other) {
return this->get_area(this) > other->get_area(other);
}
struct Rectangle* new_rectangle(double w, double h)
{
struct Rectangle* r = malloc(sizeof(struct Rectangle));
if (!r) return NULL;
r->width = w;
r->height = h;
r->get_area = &get_area;
r->compare = &compare;
return r;
}
void delete_rectangle(struct Rectangle* this)
{
free(this);
}
int main()
{
struct Rectangle* r1 = new_rectangle(1.5, 2.6);
struct Rectangle* r2 = new_rectangle(2.5, 1.6);
bool ans = r1->compare(r1, r2);
printf("%s\n", ans ? "true" : "false");
delete_rectangle(r1);
delete_rectangle(r2);
return 0;
}
get_area()話雖如此,在 C 中擁有并compare()成為 C 中的成員并沒有什么意義Rectangle:
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
struct Rectangle
{
double width;
double height;
};
double get_area(struct Rectangle* this)
{
return this->width * this->height;
}
bool compare(struct Rectangle* this, struct Rectangle* other) {
return get_area(this) > get_area(other);
}
struct Rectangle* new_rectangle(double w, double h)
{
struct Rectangle* r = malloc(sizeof(struct Rectangle));
if (!r) return NULL;
r->width = w;
r->height = h;
return r;
}
void delete_rectangle(struct Rectangle* this)
{
free(this);
}
int main()
{
struct Rectangle* r1 = new_rectangle(1.5, 2.6);
struct Rectangle* r2 = new_rectangle(2.5, 1.6);
bool ans = compare(r1, r2);
printf("%s\n", ans ? "true" : "false");
delete_rectangle(r1);
delete_rectangle(r2);
return 0;
}
uj5u.com熱心網友回復:
您的翻譯在語意上不正確:Rectangle結構不需要具有方法的函式指標。在 C 代碼中,方法不是virtual,因此編譯器根據引數的型別確定要呼叫哪些函式:將 this 轉換為 C 會給程式員帶來負擔,程式員必須更明確地命名方法并適當地呼叫它們。
這是一個更接近的翻譯(即:更接近 c 編譯器實際生成的內容):
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
typedef struct rectangle {
double width, height;
} rectangle;
rectangle *rectangle_construct(rectangle *this, double w, double h) {
this->width = w;
this->height = h;
return this;
}
rectangle *rectangle_new(double w, double h) {
return rectangle_construct(malloc(sizeof(rectangle)), w, h);
}
void rectangle_destroy(rectangle *this) {
// no members need destroying
}
void rectangle_delete(rectangle *this) {
rectangle_destroy(this);
free(this);
}
double rectangle_get_area(rectangle *this) {
return this->width * this->height;
}
bool compare(rectangle *this, rectangle *that) {
return rectangle_get_area(this) > rectangle_get_area(that);
}
int main() {
rectangle *r1 = rectangle_new(1.5, 2.6);
rectangle *r2 = rectangle_new(2.5, 1.6);
bool ans = rectangle_compare(r1, r2);
printf("%s\n", ans ? "true" : "false");
// in your code, you should add these lines
//delete r1;
//delete r2;
// emulated as
rectangle_delete(r1);
rectangle_delete(r2);
return 0;
}
uj5u.com熱心網友回復:
你弄錯了兩件事(1)結構中的函式必須將指向結構的指標作為引數。在 C 中,這是隱式發生的,在 C 中,您需要在代碼中撰寫它。不這樣做不是語法錯誤,但絕對是未定義的行為。(2) 您未能 malloc 指向您創建的結構的指標,這意味著任何取消參考都肯定是段錯誤。我進行了更正,然后您的代碼運行良好。看看這個。干杯!!!
'''
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
struct Rectangle{
double width;
double height;
void (*new_rectangle)(struct Rectangle* self, double, double);
double (*get_area)(struct Rectangle* self);
bool (*compare)(struct Rectangle* this, struct Rectangle* other);
};
void new_rectangle(struct Rectangle* self, double w, double h){
self->width = w;
self->height = h;
}
double get_area(struct Rectangle* self){
return self->width * self->height;
}
bool compare(struct Rectangle* this, struct Rectangle* other) {
return (get_area(this) > get_area(other));
}
int main(){
struct Rectangle* r1 = malloc(sizeof(struct Rectangle));
new_rectangle(r1, 1.5, 2.6);
struct Rectangle* r2= malloc(sizeof(struct Rectangle));
new_rectangle(r2, 2.5, 1.6);
bool ans = compare(r1, r2);
printf("%s\n", ans?"true":"false");
}
'''
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/448787.html
上一篇:使用指標迭代AccelStepper實體陣列(Arduino)
下一篇:沒有顯式特化宣告的顯式模板特化
