RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题

问题[классы]

Martin Hope
Владислав
Asked: 2022-09-29 15:45:25 +0000 UTC

如何制作静态子类?

  • 0

我有 2 个包含方法的静态类

public static class SMS {
    
}

和

public static class Contacts {
    
}

我想创建一个静态电话类,以便我可以这样引用它:

Phone.SMS.SendSMS(phoneNumber, text);

请告诉我该怎么做。

c# классы
  • 1 个回答
  • 50 Views
Martin Hope
Stanley
Asked: 2022-08-19 03:45:19 +0000 UTC

C ++中的复制和交换习语

  • 0

这不是我第一次提出关于折线问题的问题。这次我想在我的代码中讨论“C++ 中的复制和交换成语”。如果您有兴趣,可以在此处阅读问题说明。

因此,在我的代码中,我实现了 Copy-and-Swap 习惯用法:

void swap(Loman& a) {
        std::swap(size, a.size);
        std::swap(data, a.data);
    }
 
    Loman& operator=(const Loman& a)
    {
            Loman tmp(a);
            swap(tmp);
        return *this;
    }

但在我看来,这段代码中有一个逻辑错字,实际上应该这样写:

void swap(Loman& first, Loman& second) {
        std::swap(first.size, second.size);
        std::swap(first.data, second.data);
    }
 
    Loman& operator=(const Loman& a)
    {
            Loman tmp(a);
            swap(*this, tmp);
        return *this;
    }

在我看来, + 运算符中还有一个不必要的操作this-> size = size。如果我删除它,什么都不会改变。

这是整个代码:

#include <iostream>
using namespace std;
class Loman
{
private:
    int i=0;
    int size;
    int* data;
public:
    Loman(int asize) {
        size = 2*asize;
        data = new int[size];
    }
    ~Loman(){
        delete[] data;
        cout<<"Object destroyed\n";
    }
    Loman(const Loman &ob)
    {
        size = ob.size;
        data = new int [size];
        for(int i=0; i<ob.size; i++)
            data[i] = ob.data[i];
    }
    void print(){
        for(i=0; i<size; i++){
            cout<<*(data+i)<<" ";
        }
        cout<<endl;
    }
 
    void scan(){
        cout<<"Input "<<size/2<<" coordinates\n";
        for(int i=0; i<size; i++){
            cin>>*(data+i);
        }
    }
    Loman operator+(Loman& ob){
        this->size = size;
        Loman tmp((size + ob.size)/2);
        for(i=0; i<size; i++)
        {
            tmp.data[i]=*(data + i);//this->data[i]=data[i];
        }
        for(i=size; i<size + ob.size; i++) tmp.data[i] = ob.data[i-size];
        return tmp;
    }
    void swap(Loman& a) {
        std::swap(size, a.size);
        std::swap(data, a.data);
    }
 
    Loman& operator=(const Loman& a)
    {
            Loman tmp(a);
            swap(tmp);
        return *this;
    }
};
int main(){
    Loman first(3);
    Loman second(2);
    first.scan();
    second.scan();
    Loman third(5);
    third=first+second;
    third.print();
    return 0;
}

问题:我关于“+”运算符和交换函数的假设是否正确,如果不是,为什么不呢?
我错过了任何其他错误吗?
是否需要在swap函数中写using std::swap;

c++ классы
  • 1 个回答
  • 60 Views
Martin Hope
Davor
Asked: 2022-08-10 16:45:23 +0000 UTC

C++ 表达式必须对修改有效

  • 0
#include <iostream>

class Character
{
private:
    short x = -1;
    short y = -1;
};


class Luchnik : public Character
{
public:
    Luchnik(short x, short y) {
        this->x = x;
        this->y = y;
    }
private:
    short x = 0;
    short y = 0;
};


int main() {
    Luchnik *l = new Luchnik(1, 2);
    Character a();
    &a = l;

    delete l;
    return 0;
}

visual stuido 写入表达式必须有效才能更改左侧值。

这是屏幕截图

"

更新

或者您可以在 Luchnik 构造函数中获取一个向量指针并将 vector[x][y] 对象更改为 Luchnil

我是这样写的,但是不行,看不懂指针和类的东西

Luchnik(short x, short y, std::vector<std::vector<Character>> map) {
   this->x = x;
   this->y = y;
   Character* pm = &(map[x][y]);
   pm = *this;
} 
int main() { 
    std::vector<std::vector<Character>> board = new std::vector<std::vector<Character>>(3, std::vector<Character>(4));
    Luchnik l(1, 2, *board);
    return 0;
}
c++ классы
  • 2 个回答
  • 52 Views
Martin Hope
Zo_oM
Asked: 2022-08-07 20:54:53 +0000 UTC

C2102 - & 需要一个左撇子值 [关闭]

  • -2
关闭 这个问题是题外话。目前不接受回复。

1 个月前关闭。

  • 寻求调试帮助的问题(“为什么这段代码不起作用? ”)应该包括期望的行为、具体的问题或错误,以及在问题中重现它的最少代码。没有明确描述问题的问题对其他访问者毫无用处。请参阅如何创建一个最小的、独立的和可重现的示例。
  • 该问题是由不再复制的问题或错字引起的。虽然类似问题可能与本网站相关,但该问题的解决方案不太可能帮助未来的访问者。通常可以通过在发布问题之前编写和研究一个最小程序来重现问题来避免此类问题。
改进问题

书中的代码略有更新。我不知道为什么它会出错,最令人惊讶的是,它会在不是我编写的代码中出错!

#include <iostream>
using namespace std;

char* mycpy(char* word_1, const char* word_2) {
    while (*word_1++ = *word_2++);
    return word_1;
}

class TwoDShape {
    double width, height;
    char name[20];
public:
    TwoDShape() {
        width = height = 0, 0;
        mycpy(name, "no");
    }
    TwoDShape(double w, double h, const char* n) {
        width = w;
        height = h;
        mycpy(name, n);
    }
    TwoDShape(double x, const char* n) {
        width = height = x;
        mycpy(name, n);
    }

    void showDim() {
        cout << "Ширина и высота составляют " << width << "и " << height << "\n";
    }

    double getWidth() { return width; }
    double getHeight() { return height; }
    void setWigth(double w) { width = w; }
    void setHeight(double h) { height = h; }
    void setName(const char* n) { mycpy(name, n); }
    char* getName() { return name; }

    virtual double area() = 0;
};

class Triangle : public TwoDShape {
    char style[20];
public:
    Triangle() {
        mycpy(style, "no");
    }
    Triangle(const char* str, double w, double h) : TwoDShape(w, h, "треугольник") {
        mycpy(style, str);
    }
    Triangle(double x) : TwoDShape(x, "треугольник") {
        mycpy(style, "равнобедренный");
    }

    void showStyle() {
        cout << "Треугольник " << style << "\n";
    }
    double area() {
        return getWidth() * getHeight() / 2;
    }
};

class Rectangle : public TwoDShape {
public:
    Rectangle(double w, double h) : TwoDShape(w, h, "Прямоугольник"){ }
    Rectangle(double x) : TwoDShape(x, "Прямоугольник") { }

    bool isSquare() {
        if (getWidth() == getHeight()) return true;
        return false;
    }
    double area() {
        return getWidth() * getHeight();
    }
};

class Circle : public TwoDShape {
    double R;
public:
    Circle(double r) : TwoDShape() { 
        R = r;
        setName("Кргу");
    }

    double area() {
        return (3.14 * (R * R));
    }
};

int main() {
    setlocale(LC_ALL, "ru");
    TwoDShape* p_shapes[5];

    p_shapes[0] = &Triangle("Прямоугольный", 8.0, 12.0); // <------------ Ошибка
    p_shapes[1] = &Rectangle(10);// <------------ Ошибка
    p_shapes[2] = &Rectangle(10, 4);// <------------ Ошибка
    p_shapes[3] = &Triangle(7.0);// <------------ Ошибка
    p_shapes[4] = &Circle(33.0);// <------------ Ошибка

    for (int i = 0; i < 4; i++) {
        cout << "Объект представляет собой " << p_shapes[i]->getName() << "\n";
        cout << "Площадь равна " << p_shapes[i]->area() << "\n";
        cout << "\n";
    }
}
c++ классы
  • 1 个回答
  • 103 Views
Martin Hope
LShadow77
Asked: 2022-08-06 21:44:07 +0000 UTC

嵌套模板的问题

  • 1

假设我们有一些课程:

class Base
{
public:
    INLINE static int get_something()
    {
        return 777;
    }
    
    //....
};

并且还声明了以下模板类 A:

template <class TBase>
class A
{
public:
    template <typename TObject>
    INLINE static TObject get_something()
    {
        return (TObject)(TBase::get_something() + 1);
    }

    //....
};

现在我们可以在这样的代码中使用它:

double a = A<Base>::get_something<double>();
printf("A: %lf\n", a);

太好了,一切正常!但是,现在让我们添加一个模板类 B,它可以被认为是 A 的某种包装器:

template <class TBase>
class B
{
public:
    template <typename TObject>
    INLINE static TObject get_something()
    {
        return (TObject)(A<TBase>::get_something<TObject>() + 1); //Тут компилятор ругается!
    }

    //....
};

我们立即从编译器中得到:

main.cpp: In static member function 'static TObject B<TBase>::get_something()':
main.cpp:38:57: error: expected primary-expression before '>' token
   38 |         return (TObject)(A<TBase>::get_something<TObject>() + 1);
      |                                                         ^
main.cpp:38:59: error: expected primary-expression before ')' token
   38 |         return (TObject)(A<TBase>::get_something<TObject>() + 1);

同时,如果在表达式中我们A<TBase>::get_something<TObject>()将模板参数替换TBase为显式类型Base,那么一切都会再次起作用:

template <class TBase>
class B
{
public:
    template <typename TObject>
    INLINE static TObject get_something()
    {
        return (TObject)(A<Base>::get_something<TObject>() + 1); //А тут всё хорошо...
    }

    //....
};

//...и мы можем это использовать в коде
float b = B<Base>::get_something<float>();
printf("B: %f\n", b);

问题是,编译器不喜欢什么以及如何使其正确取消引用类模板参数A,其函数是从模板类B的模板函数调用的?类似的东西。

c++ классы
  • 1 个回答
  • 22 Views

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    我看不懂措辞

    • 1 个回答
  • Marko Smith

    请求的模块“del”不提供名为“default”的导出

    • 3 个回答
  • Marko Smith

    "!+tab" 在 HTML 的 vs 代码中不起作用

    • 5 个回答
  • Marko Smith

    我正在尝试解决“猜词”的问题。Python

    • 2 个回答
  • Marko Smith

    可以使用哪些命令将当前指针移动到指定的提交而不更改工作目录中的文件?

    • 1 个回答
  • Marko Smith

    Python解析野莓

    • 1 个回答
  • Marko Smith

    问题:“警告:检查最新版本的 pip 时出错。”

    • 2 个回答
  • Marko Smith

    帮助编写一个用值填充变量的循环。解决这个问题

    • 2 个回答
  • Marko Smith

    尽管依赖数组为空,但在渲染上调用了 2 次 useEffect

    • 2 个回答
  • Marko Smith

    数据不通过 Telegram.WebApp.sendData 发送

    • 1 个回答
  • Martin Hope
    Alexandr_TT 2020年新年大赛! 2020-12-20 18:20:21 +0000 UTC
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5