小编Ale*_*oft的帖子

不明确使用operator double()

我有一个Rectangle类转换运算符,double并且std::string:

class Rectangle
{
public:
    Rectangle(double x, double y) : _x(x), _y(y) {}
    operator std::string ();
    operator double ();
private:
    double _x, _y;
    double getArea() {return _x * _y;}
};

int main()
{
    Rectangle r(3, 2.5);
    cout << r << endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我不明白为什么operator double()被调用,而不是operator std::string().据我所知,根据C++ wikibook, operator double用于将Rectangle对象转换为double.

那么这里发生了什么?是否与int传递给构造函数的事实有关?如果是这样,为什么?

c++ operator-overloading implicit-conversion

15
推荐指数
2
解决办法
6118
查看次数

由于没有主模块,因此不会生成任何输出

我写了一个简单的模块叫做Test:

module Test (main) where

main =
  putStrLn "Hello"
Run Code Online (Sandbox Code Playgroud)

但是,当我尝试通过以下命令行编译它时:

ghc Test.hs -o my-program
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

[1 of 1] Compiling Test             ( Test.hs, Test.o )

<no location info>: error:
    output was redirected with -o, but no output will be generated because there is no Main module.
Run Code Online (Sandbox Code Playgroud)

haskell program-entry-point

14
推荐指数
2
解决办法
1530
查看次数

调用虚方法而不指向对象?

#include <iostream>

struct A {
    void init()
    {
        internal_init();
    }
    virtual void internal_init()
    {
        std::cout << "internal of A" << std::endl;
    }
};

struct B: public A {
    void internal_init()
    {
        init();
        std::cout << "internal of B" << std::endl;
    }
};

int main(){
    B instance;
    std::cout << "internal of A" << std::endl;
    instance.internal_init();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

首先,该计划B::internal_init()按预期进行.然后,到A::init()(我猜因为B来自A,而B没有任何init()).怎么办?

什么internal_init()它会选择谁?因为它进入B::internal_init(),程序将进入无限循环,我不明白为什么.

  • 我打电话时真的发生了什么internal_init()
  • 为什么它调用internal_init()实例的"B部分"?这是关于"虚拟"吗?如果是这样,怎么样?虚函数在我们使用多态时发生(就像我自己理解的初学者一样,它使用指向派生类对象的基类的指针).

c++ inheritance virtual-functions

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

为什么引用类型的某些变量可以绑定rvalues而有些不能?

#include <iostream>

using namespace std;
int main() {

//int& a = 3; <- Doesn't compile. Expression must be lvalue.
const auto& c = 1 + 2; // c is a constant reference to an int. (?)
                       // compiles fine. 1+2 is a rvalue? what's going on?
cout << c << endl;

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

我不明白为什么编译器不会引发编译错误.由于auto"强制"c是对常量int的引用,并且引用被引用到左值,为什么它会起作用?

c++ c++11

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

无法理解fork()输出

我们假设有一个进程,PID = 1它运行以下代码:

int a = fork();
int b = fork();
printf(“a: %d, b: %d\n”, a, b); 
Run Code Online (Sandbox Code Playgroud)

让我们进一步假设,新的PID旨意被赋予一个接一个,如此发出第二个PID2,然后3

可能的输出是:

a:2, b:3
a:2, b:0
a:0, b:4
a:0, b:0
Run Code Online (Sandbox Code Playgroud)

我有一些麻烦试图理解上面代码的输出,尤其是为什么a:0, b:4a:2, b:3.

c fork pid

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

静态引用引用临时变量

#include <iostream>

int getID ( int k ) {
    static int& r = k;
    return r++;
}

int main()
{
    int a = getID ( 10 );
    int b = getID ( 10 );
    std::cout << "a = " << a << ", b = " << b  << std::endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我不明白为什么这个代码编译.

  1. 静态引用如何引用局部变量k,它将在函数调用结束时消失.
  2. 在第二次调用时,我们使用新变量重新初始化静态引用.请解释一下这里发生了什么,为什么静态REFERENCE可以"重新定义"(我想我不理解方法中对静态变量的引用的含义).

c++

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

单个读取器多个写入器与pthreads和锁定,没有提升

考虑下一段代码.

#include <iostream>
#include <vector>
#include <map>

using namespace std;

map<pthread_t,vector<int>> map_vec;
vector<pair<pthread_t ,int>> how_much_and_where;

pthread_cond_t CV = PTHREAD_COND_INITIALIZER;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;

void* writer(void* args)
{
    while(*some condition*)
    {
        int howMuchPush = (rand() % 5) + 1;
        for (int i = 0; i < howMuchPush; ++i)
        {
            // WRITE
            map_vec[pthread_self()].push_back(rand() % 10);
        }

        how_much_and_where.push_back(make_pair(pthread_self(), howMuchPush));
        // Wake up the reader - there's something to read.
        pthread_cond_signal(&CV);
    }

    cout << "writer thread: " <<  pthread_self()  << endl;
    return nullptr;
} …
Run Code Online (Sandbox Code Playgroud)

c++ multithreading pthreads producer-consumer

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

根据每个像素的位置有效地在每个像素上应用一个函数

我想有效地迭代给定图像的每个像素,并根据像素的位置,应用一个函数并将输出放在这个位置。

这是我尝试过的:

def point_gaussian_value(p1, p2, sigma=0.8):
    x1, y1 = p1
    x2, y2 = p2

    return np.exp(-1 * (np.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2) / sigma ** 2))

point_annotation = np.zeros_like(sample).astype(np.float32)
for j in range(sample.shape[0]):
    for k in range(sample.shape[1]):

        value = point_gaussian_value(p1=(j, k), p2=(row[header[i]], row[header[i + 1]]))
        point_annotation[j, k] = point_gaussian_value(p1=(j, k), p2=(20, 20))
Run Code Online (Sandbox Code Playgroud)

尽管它很幼稚,而且效率极低,特别是对于大图像。我知道我可以矢量化一个函数,这样所有计算都将同时有效地完成,但我不确定它是否可能/如何矢量化一个函数,以获得每个像素的像素位置。

python performance numpy image-processing

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

使用多态指针调用函数

#include <iostream>
class A{};
class AA:public A{};

struct X {
    void f(A *) {
        std::cout << "X::f(A*)\n";
    }
};

struct Y:public X {
    void f(A *) {
        std::cout << "Y::f(A*)\n";
    }
};

int main() {
    Y *y = new Y();
    X *x = new Y();
    const X *cx = new Y();

    y->X::f(new AA);
    x->f(new AA);
}
Run Code Online (Sandbox Code Playgroud)

打印:

X :: F(A*)

X :: F(A*)

我不明白为什么y->X::f(new AA),x->f(new AA)不会引起编译错误.X::f(A *)在两种情况下我都会调用它.但编译器选择使用此方法的原则是什么?函数重载多态?任何拇指规则?

c++ inheritance

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

在没有值的情况下向std :: map添加键

假设我定义了一张全球地图 -

map<int,list<char>> cMap;
Run Code Online (Sandbox Code Playgroud)

有没有办法(不使用boost)我可以添加整数键,稍后在程序中添加值到与它们对应的列表?

map<int,list<char>> cmap;

int main()
{

// Add only a value this way?
cmap[2];

// and then -
cmap[2].push_back('A');
return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++

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

EditText 中的 InputType 改变了 rtl 文本的重力

<EditText
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textColorHint="#E6E6E6"
    android:inputType="textPassword"
    android:hint="?????"
    android:id="@+id/etLoginPassword"
    android:layout_marginEnd="30dp"
    android:layout_marginStart="30dp"
    android:singleLine="true"
    android:imeOptions="actionDone"/>
Run Code Online (Sandbox Code Playgroud)

当我改变时android:inputTypeTextHint它“扔”到左边。当我将重力更改为“正确”时,它会返回到它需要的位置(希伯来语)。但是,我希望密码(用 EditText 编写的文本)重力为“左”(密码应该用英文字符书写)。我怎样才能克服这个问题?

android

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

创建数据类型和monad时出错和混淆

考虑下一段代码:

data Tile   = EmptyTile | X | O
data Player = Player1 | Player2

instance Show Tile where
    show EmptyTile = " "
    show X         = "X"
    show O         = "O"

data Board = (Tile, Tile, Tile, Tile, Tile, Tile, Tile, Tile, Tile)

emptyBoard :: Board
emptyBoard = (EmptyTile,EmptyTile,EmptyTile,EmptyTile,EmptyTile,EmptyTile,EmptyTile,EmptyTile,EmptyTile)

instance Monad Board where
    return x = x
    f >>= x = x
Run Code Online (Sandbox Code Playgroud)

我希望那个董事会成为一个单子.但问题是我得到以下错误 -

[1 of 1] Compiling Main             ( Main.hs, Main.o )

Main.hs:17:14: error:
    Illegal binding of built-in …
Run Code Online (Sandbox Code Playgroud)

haskell

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