小编Eit*_*ron的帖子

C++构造函数的问题

编辑:这个问题出现了,我想我已经开始了!去StackOverflow !! :d

我的考试即将开始,去年考试的一个问题是通过实施以下构造函数来发现问题,并编写一个更正的问题.

Rectangle::Rectangle(string col, int len, int br)
{
    setColour(col);
    length =len;
    breadth=br;
}
Run Code Online (Sandbox Code Playgroud)

类定义如下:

class Polygon
{
public:
    Polygon(string col="red");
    void printDetails(); // prints colour only
    virtual double getArea()=0;
    void setColour(string col);
private:
    string colour;
};


class Rectangle : public Polygon
{
public:
    Rectangle(string, int, int);
    void printDetails(); // prints colour and area
    // for part 3, delete the line below
    double getArea();
private:
    int length;
    int breadth;
};
Run Code Online (Sandbox Code Playgroud)

我已将代码写入编译器,运行正常.我猜这个问题与继承有关,因为它string colour;是私有的,但是setColour是公开的,所以我无法弄明白.除非它Rectangle::Rectangle(string …

c++ constructor

37
推荐指数
5
解决办法
900
查看次数

如何解决功能模板的局部特化?

例如,我有一个班级:

class A
{
    enum {N = 5};
    double mVariable;

    template<class T, int i>
    void f(T& t)
    {
        g(mVariable); // call some function using mVariable.
        f<T, i+1>(t); // go to next loop
    }

    template<class T>
    void f<T, N>(T& t)
    {} // stop loop when hit N.
};
Run Code Online (Sandbox Code Playgroud)

功能模板中不允许部分特化.在我的情况下,我该如何解决这个问题?

我略微改变了Arne Mertz的例子,如:

template<int n>
struct A
{
    enum {N = n};
    ...
};
Run Code Online (Sandbox Code Playgroud)

并使用A像:

A<5> a;
Run Code Online (Sandbox Code Playgroud)

我无法在Visual Studio 2012上编译.它是编译器错误还是其他什么?这很奇怪.

编辑:检查.这是一个Visual Studio错误.:(

我认为Nim提供了实现它的最简单方法.

c++ templates template-specialization

10
推荐指数
2
解决办法
922
查看次数

C++运算符在表达式中重载

我确信这已经在某个地方得到了解答,但我不知道该搜索什么.

我有以下情况.我创建了一个Vector类并重载了"*"(乘以escalar)和"+"运算符(添加两个向量).现在,以下代码行:

Vector sum = (e_x*u_c) + (e_y*u_r);
Run Code Online (Sandbox Code Playgroud)

这给了我以下错误:

error: no match for 'operator+' in '((Teste*)this)->Teste::e_x.Vector::operator*(u_c) + ((Teste*)this)->Teste::e_y.Vector::operator*(u_r)'
Run Code Online (Sandbox Code Playgroud)

但是,如果我将此错误行替换为:

Vector aux = (e_x*u_c);
Vector aux2 = (e_y*u_r);
Vector sum = aux + aux2;
Run Code Online (Sandbox Code Playgroud)

我没有任何错误.为什么?这两个表达不是等同的吗?

编辑:这是我的"*"和"+"的定义:]

Vector Vector::operator+(Vector& right)
{
    return Vector(x + right.x, y + right.y, z + right.z);
}
double Vector::operator*(Vector& right)
{
    return this->scalar_product(right);
}
Run Code Online (Sandbox Code Playgroud)

c++ expression overloading operator-keyword

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

在 C++ 的 SFML 中将形状/对象居中

所以最近,我开始使用 SFML 在 Visual Studio 中制作游戏。设置完所有内容并编写一些示例代码后,我设计了以下内容:

#include <SFML/Graphics.hpp>

int main()
{
    sf::RenderWindow window(sf::VideoMode(600, 600), "Move the Shape");
    sf::CircleShape shape(100.f);
    shape.setFillColor(sf::Color::Green);
    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();
        }

        window.clear();
        window.draw(shape);
        window.display();
    }

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

该程序产生以下结果: 移动未居中的形状

如何将圆放在中心?我想在之后设置一些代码,让用户使用键盘的箭头键移动圆圈,所以我需要将圆圈放在中心。

c++ visual-c++ sfml visual-studio-2012

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

一类中无限数量的成员变量?C++

对于我GroupPicker程序,我需要允许class group有每个被分配到进入每个学生随机生成的数字插槽无限多.代码:

#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
#include <time.h>

using namespace std;

class student
{
    int m_studentNumber;
public:
    string nameFirst;
    string nameLast;
    string nameFull;
    int getStudentNumber() { return m_studentNumber; }
    void setStudentNumber(int studentNumber) { m_studentNumber = studentNumber; }
};

class group
{
public:
};

ostream& operator<<(ostream& os, const student& s)
{
     return os << s.nameFirst << ' ' << s.nameLast;
}

student typeName()
{
    student bar;
    cout << "Type in a student's first name: "; …
Run Code Online (Sandbox Code Playgroud)

c++ random class

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

在C++中使用C关键字

所以我想知道为什么C++允许使用C关键字.毕竟,printf("Hello, World!\n");做同样的事情cout << "Hello, World!\n";.一个人比另一个人更快地回到了过程中吗?或者你在C++中可能需要C中的一些关键字?(PS:这个问题出于纯粹的好奇心)

c c++ keyword difference

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