小编bar*_*rej的帖子

c ++ struct operator:请求从非标量类型的转换

我想要做的是定义一个结构等于运算符。但这似乎有什么问题。如何修复此代码?

struct Rectangle
{
public:
    double w;
    double h;
    Rectangle& operator=(int wh)
    {
        w=wh;
        h=wh;
        return *this;
    } 
};

int main()
{
    Rectangle rect=5;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

命令:

$ g++ -std=c++11 test.cpp
Run Code Online (Sandbox Code Playgroud)

错误:

test.cpp: In function ‘int main()’:
test.cpp:16:17: error: conversion from ‘int’ to non-scalar type ‘Rectangle’ requested
  Rectangle rect=5;
                 ^
Run Code Online (Sandbox Code Playgroud)

c++ struct operator-overloading type-conversion

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

使用 jquery 获取 URL 内容

我正在使用读取本地文件的内容,jquery我得到undefined.

我觉得问题与 URL 加载的同步有关。但我不知道如何修复此代码:

function url_content(url)
{
    var content;
    $.get(url,function( data ) {content=data});
    return content;
}


var content=url_content("local_templates/template1.html");
alert(content);
Run Code Online (Sandbox Code Playgroud)

javascript url jquery curl

2
推荐指数
2
解决办法
3万
查看次数

从“const int*”到“int*”的无效转换

我收到以下错误

\n\n
$ g++ test.cpp\ntest.cpp: In function \xe2\x80\x98int test1(const int**, int)\xe2\x80\x99:\ntest.cpp:11:14: error: invalid conversion from \xe2\x80\x98const int*\xe2\x80\x99 to \xe2\x80\x98int*\xe2\x80\x99 [-fpermissive]\n         a=v[i];\n              ^\ntest.cpp: In function \xe2\x80\x98int main()\xe2\x80\x99:\ntest.cpp:31:20: error: invalid conversion from \xe2\x80\x98int**\xe2\x80\x99 to \xe2\x80\x98const int**\xe2\x80\x99 [-fpermissive]\n     cout<<test1(c,2)<<endl;\n                    ^\ntest.cpp:4:5: error:   initializing argument 1 of \xe2\x80\x98int test1(const int**, int)\xe2\x80\x99 [-fpermissive]\n int test1(const int **v,int num)\n     ^\n
Run Code Online (Sandbox Code Playgroud)\n\n

编译以下代码时:

\n\n
#include <iostream>\nusing namespace std;\n\nint test1(const int **v,int num)\n{\n    int *a;\n    int result=0;\n    // do somethings ....\n    for(int i=0;i<num;i++)\n    {\n        a=v[i];\n        // do somethings ....\n …
Run Code Online (Sandbox Code Playgroud)

c++ pointers casting compiler-errors

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

错误:聚合'food_type a'的类型不完整,无法定义

编译器不允许我运行此代码:

错误:聚合'food_type a'的类型不完整,无法定义

除非我加括号()实例化后a,f1,f2,f3防止cout功能显示任何东西.为什么会这样?

#include <iostream>

enum class Animal { Cat, Dog, Duck};

class Food
{
public:
    Food()
    {
        std::cout<<"Food called"<<std::endl;
    }
};

template <enum Animal,class food_type>
class Ecosystem;

template<>
class Ecosystem<Animal::Cat,class food_type>
{
public:
    Ecosystem()
    {
        std::cout<<"Cat constructor called"<<std::endl;
        food_type a;
    }
};

template <>
class Ecosystem<Animal::Dog,class food_type>
{
public:
    Ecosystem()
    {
        std::cout<<"Dog constructor called"<<std::endl;
        food_type a;
    }
};


template <>
class Ecosystem<Animal::Duck,class food_type>
{
public:
    Ecosystem() …
Run Code Online (Sandbox Code Playgroud)

c++ templates c++11

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

c ++ auto with overriding signed/unsigned

在以下代码中:

#include <armadillo>

using namespace arma;

int main()
{
    mat A;
    auto x=A.n_rows-5;
    ....
Run Code Online (Sandbox Code Playgroud)

x是的long long unsigned int,我想要它 long long int.我该如何解决这个问题?

应该注意的是,在这个库的不同版本中,使用了不同的类型,所以我不能long long int直接提及我需要使用auto.

c++ types armadillo c++11

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

c ++将无穷大转换为max,将-inf转换为min

如何实施以下行动?

if( boost::math:: +is_inf (x) )
    x= max double;
else if( boost::math:: -is_inf (x) )
    x= min double;
Run Code Online (Sandbox Code Playgroud)

我想保留x的符号.

c++ double boost c++11

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