相关疑难解决方法(0)

在成员函数中调用类构造函数

这是一个程序,我试图multi::multi(int, int)在函数中调用类构造函数void multi::multiply().输出是

三十

三十

而不是预期的

三十

25

为什么?

#include <iostream.h>

class multi{
    private:
        int a;
        int b;
    public:
        multi(int m, int n){
            a = m;
            b = n;
        }

        void multiply(){
            cout << "\n\n" << a*b;
            multi (5, 5);
            cout << "\n" << a*b;
        }
};

main(){
    multi x(5,6);

    x.multiply();
return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++ constructor function

5
推荐指数
1
解决办法
1万
查看次数

这个构造函数定义的好处是什么

我刚刚浏览了Cprogramming.com上的随机页面,并注意到了Constructors and Destructors教程/示例页面.他们使用了以下定义构造函数的方法:

class String
{
    private:
        char *str;
        int size;
    public:
        String() : str(NULL), size(0) { }  // <- This statement
        String(int size) : str(NULL), size(size) {  // <- And this one
            str = new char[size];
        }
}
Run Code Online (Sandbox Code Playgroud)

我一直在使用带有魔术this指针的构造函数的老式定义:

String() { 
    this->str = NULL;
    this->size = 0;
}
String(int size) {
    this->size = size;
    this->str = new char[size];
}
Run Code Online (Sandbox Code Playgroud)

除了明显较小的代码(较少的行数)之外,第一个声明中是否还有其他好处?

PS:自从我上次用C++编写内容以来已经有好几年了.

c++ constructor coding-style class

5
推荐指数
1
解决办法
209
查看次数

是作者炫耀还是真实的做法

我正在研究Michael J Laszlo的书" C++中计算几何和计算机图形学 ".以下是模板类原型:

template <class T> class ListNode : public Node {    
    T _val;
    ListNode (T val);
    friend class List<T>;
};

template <class T> ListNode <T>::ListNode(T val)
{_val=val;};

template <class T> class List{    
  private:
    ListNode <T> *header;
    ListNode <T> *win;
    int _length;

  public:
    List(void);
    ~List(void);
    T insert(T);
    T append(T);
    List * append(List*);
    T prepend(T);
    T remove(void);
    void val(T); // overloaded function!
    T val(void);// overloaded function!
    T next(void);
    T prev(void);
    T first(void);
    T last(void);
    int length(void);
    bool …
Run Code Online (Sandbox Code Playgroud)

c++ templates

4
推荐指数
2
解决办法
414
查看次数

编译器无法看到参数

我是C++的新手,想要上课.

我的代码

在我的世界里有英雄和剑.英雄携带剑.这不应该太难.

// Defining swords                                                                                                                                 
class Sword
{
  // The most important thing about a sword is its length.                                                                                         
  int lenght;
public:
  // only constructor and destructor                                                                                                               
  Sword(int swordlength){
    lenght = swordlength;
  };
  ~Sword(){};
};

// defining heros (as people with magic swords)                                                                                                    
class Hero
{
  Sword magic_sword;
public:
  // each hero gets a standard sword                                                                                                               
  Hero(){
    int meters = 2;
    magic_sword = Sword(meters);
  };
  ~Hero(){};
};

int main(){
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

编译器对它的看法

当我编译这段代码(g++ hero.cpp)时,我收到一个错误:

 In constructor …
Run Code Online (Sandbox Code Playgroud)

c++ constructor class

4
推荐指数
1
解决办法
71
查看次数

构造函数定义C++语法

这两个构造函数声明之间有什么区别:

class Fruit {
  private:
    int price;

  public:
    Fruit(int x): price(x)
    {
    }    
};
Run Code Online (Sandbox Code Playgroud)

VS

class Fruit {
  private:
    int price;

  public:
    Fruit(int x)
    {
        price = x;
    }
};
Run Code Online (Sandbox Code Playgroud)

在继承的情况下我见过的第一个.

据我所知,这不是一个重复的问题.如果你发现一个人可以随意关闭这个问题.

c++ oop ctor-initializer c++11

4
推荐指数
2
解决办法
118
查看次数

了解和使用副本分配构造函数

我试图了解副本分配构造函数在c ++中的工作方式。我只使用过Java,所以我真的不在这里。我已经阅读并看到返回引用是一种很好的做法,但是我不知道该怎么做。我写了这个小程序来测试这个概念:

main.cpp:

#include <iostream>
#include "test.h"

using namespace std;

int main() {
    Test t1,t2;
    t1.setAge(10);
    t1.setId('a');
    t2.setAge(20);
    t2.setId('b');

    cout << "T2 (before) : " << t2.getAge() << t2.getID() << "\n";

    t2 = t1; // calls assignment operator, same as t2.operator=(t1)

    cout << "T2 (assignment operator called) : " << t2.getAge() << t2.getID() << "\n";

    Test t3 = t1; // copy constr, same as Test t3(t1)

    cout << "T3 (copy constructor using T1) : " << t3.getAge() << t3.getID() << …
Run Code Online (Sandbox Code Playgroud)

c++ copy-assignment

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

初始化列表中的 C++ 向量集大小或调整大小

以下哪一项是首选/更有效的?

我在生产代码中都见过,但我也听说如果你可以将一些工作委托给编译器,你应该这样做,所以我总是喜欢第一个。

其中一个比另一个更好吗?

c++ c++14

3
推荐指数
1
解决办法
1382
查看次数

如何在没有复制构造函数和赋值运算符的情况下实例化实例变量

我正在使用一个类,该类没有实现赋值运算符,并且禁用了复制构造函数.我可以实例化一个像这样LibraryClass命名的本地实例var:

LibraryClass var(data, (char *)fileName, results);
Run Code Online (Sandbox Code Playgroud)

但我想LibraryClass在我写的类上创建一个实例变量.然后我想在类构造函数中实例化它.像这样的东西:

class MyClass
{
    LibraryClass var;
    void MyClass();
}

MyClass::MyClass()
{
    var = LibraryClass(data, (char *)fileName, results);
}
Run Code Online (Sandbox Code Playgroud)

在这种情况下,我最终得到了

error: ‘LibraryClass& LibraryClass::operator=(const LibraryClass&)’ is private
LibraryClass& operator=(const LibraryClass& rOther);  // no implementation
Run Code Online (Sandbox Code Playgroud)

我已经尝试了所有我能想象的工作,但没有任何工作.我试图做的甚至可能吗?我没有想法,所以任何建议都非常感谢.

编辑

我实际上并没有在构造函数中实例化变量.它发生在一个单独的功能中.我只说构造函数,因为我错误地认为它只是一个简化的假设.我没有意识到初始化列表会解决这个问题.我想回答的主要问题是标题.

如何实例化没有复制构造函数或赋值运算符的类的实例变量?或者是初始化列表是唯一的方法吗?

c++

2
推荐指数
1
解决办法
1303
查看次数

对象中的C++对象

我正在从OOP角度编写一个用于C++的Arduino程序并遇到一个问题:类的方法无法看到该类的构造函数中定义的对象.我试图完成的是创建一个对象(类),用于容纳各种方法,这些方法用于计算和输出DHT11传感器的数据.完整代码:

* DhtSensor.h
 *
 *  Created on: 2017-04-18
 *      Author: Secret
 */

#ifndef DhtSensor_h
#define DhtSensor_h

class DhtSensor {
public:
    DhtSensor(int dhtPin); //constructor
    void read();
    void printToScreen(); //a method for printing to LCD

private:
    unsigned long previousMillis; //Millis() of last reading
    const long readInterval = 3000; //How often we take readings
    float readingData[2][30]; //store current ant last values of temperature [0] and humidity [1] readings
    int readingIndex;
    bool initDone; //Bool value to check if initialization has been complete …
Run Code Online (Sandbox Code Playgroud)

c++ arduino

2
推荐指数
1
解决办法
403
查看次数

C++从函数返回struct值

因此,对于我的一个任务,我必须使用结构生成随机图形圆和矩形.但我无法理解如何从一个函数输出结构.

   struct Circle{
   int x;
     int y;
     int radius;
     int r;
     int g;
     int b;
   };

   Circle createCirc() {
     int x = rand() % window_Width;
     int y = rand() % window_Height;
     int radius = rand() % 100;
     int r = rand()%256;
     int g = rand()%256;
     int b = rand()%256;
     return Circle(x,y,radius,r,g,b);
   }
Run Code Online (Sandbox Code Playgroud)

在这里我用对象的基本值创建结构,然后我将一些数据从main传递给这个函数.

 Circle circle[1000];
 circle[count] = createCirc();
Run Code Online (Sandbox Code Playgroud)

但是,我甚至无法让它运行,因为显然在定义结构本身时它带有这个错误:

main.cpp:47:8:注意:候选构造函数(隐式移动构造函数)不可行:需要1个参数,但是提供了6个

我只是不明白如何将函数中的数据传递给main中的varable.

c++

2
推荐指数
1
解决办法
85
查看次数