小编sud*_*008的帖子

指针或参考

我正在构建一个用于处理复杂数字的c ++程序.我遇到此代码的行为有问题:

Complex& Complex::operator=(const Complex& com)
{
    Complex::re_=com.re_;
    Complex::im_=com.im_;
    return *this;
}
Run Code Online (Sandbox Code Playgroud)

现在,此函数的返回类型是Complex类型的引用.所以我不应该过去this而不是*this.

c++ oop reference this

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

字符串和int上的+运算符

我正在使用gcc 4.7.2

使用以下代码:

#include <iostream>
using namespace std;

int main()
{
    string x="hello";
    int y=1;
    x=x+y;
    cout<<x;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我收到错误:

g++     test.cpp   -o test
test.cpp: In function ‘int main()’:
test.cpp:8:6: error: no match for ‘operator+’ in ‘x + y’
test.cpp:8:6: note: candidates are:
In file included from /usr/include/c++/4.7/bits/stl_algobase.h:68:0,
                 from /usr/include/c++/4.7/bits/char_traits.h:41,
                 from /usr/include/c++/4.7/ios:41,
                 from /usr/include/c++/4.7/ostream:40,
                 from /usr/include/c++/4.7/iostream:40,
                 from test.cpp:1:
/usr/include/c++/4.7/bits/stl_iterator.h:335:5: note: template<class _Iterator> std::reverse_iterator<_Iterator> std::operator+(typename std::reverse_iterator<_Iterator>::difference_type, const std::reverse_iterator<_Iterator>&)
/usr/include/c++/4.7/bits/stl_iterator.h:335:5: note:   template argument deduction/substitution failed:
test.cpp:8:6: note:   mismatched types ‘const std::reverse_iterator<_Iterator>’ …
Run Code Online (Sandbox Code Playgroud)

c++ string gcc

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

用c ++映射初始化以实现尝试

试图在c ++中实现尝试.这是我使用的结构:

typedef struct tries{
    int wordCount;
    int prefixCount;
    map<int,struct tries*> children;
}tries;
Run Code Online (Sandbox Code Playgroud)

初始化方法:

void initialise(tries *vertex)
{
    vertex = (tries*)malloc(sizeof(tries*));
    vertex->wordCount = vertex->prefixCount = 0;
    for(char ch='a';ch<='z';ch++)
        vertex->children[ch]=NULL;

}
Run Code Online (Sandbox Code Playgroud)

初始化方法具有分段故障vertex->children[ch]=NULL;该故障是:

Program received signal SIGSEGV, Segmentation fault.
0x000000000040139a in std::less<int>::operator() (this=0x604018, 
    __x=@0x21001: <error reading variable>, __y=@0x7fffffffddb8: 97)
    at /usr/include/c++/4.6/bits/stl_function.h:236
236           { return __x < __y; }
Run Code Online (Sandbox Code Playgroud)

怎么了?

c++ pointers

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

在verilog中实现PIPO

我期待在verilog HDL中实现32位并行并行输出.这是我写的代码......

module pipo(input_seq, answer,reset, clock);
   input [31:0] input_seq;
   input        reset,clock;
   output [31:0] answer;

   always @ (reset)
     begin
        if(!reset)
          begin
             answer[31:0]<=1'b0;
          end
     end

   always @ (posedge clock)
     begin
        answer[31:1]<=input_seq[30:0];  
     end

endmodule
Run Code Online (Sandbox Code Playgroud)

但是,这会导致以下错误日志(使用iverilog):

pipo.v:10: error: answer['sd31:'sd0] is not a valid l-value in pipo.
pipo.v:4:      : answer['sd31:'sd0] is declared here as wire.
pipo.v:16: error: answer['sd31:'sd1] is not a valid l-value in pipo.
pipo.v:4:      : answer['sd31:'sd1] is declared here as wire.
Elaboration failed
Run Code Online (Sandbox Code Playgroud)

有什么问题?

verilog icarus

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

在函数实现中传递默认参数

我正在使用默认参数定义一个函数.

state* construct_state(int final_state=0, int start_state=0)      
{
      //code
}
Run Code Online (Sandbox Code Playgroud)

但是有一些错误

nfa.c:16:39: error: expected ‘;’, ‘,’ or ‘)’ before ‘=’ token
Run Code Online (Sandbox Code Playgroud)

我哪里错了?

c

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

Bizzare输出来自substr

我试图从一个看起来像cs600032016s的代码中提取年份.年份从索引7到10,但只是为了概括它前面的代码的可变长度,我使用了以下代码:

$year = substr($code, $len-5, $len-1);
Run Code Online (Sandbox Code Playgroud)

但它给了我2016s而不是2016年.任何想法错误的地方?更改$len-1$len-2$len-3没有给出改变输出.这很奇怪.这是代码:

sub getCCodeFromCode{
  my ($code) = @_;
  my $len = length($code);
  return substr($code, 0 , $len-5);    #4 char for year and one for sem
}

sub getYearFromCode{
  my ($code) = @_;
  my $len = length($code);
#  print "substr($code, $len-5, $len-1)";
  return substr($code, $len-5, $len-1);
}

sub getSemFromCode{
  my ($code) = @_;
  my $len = length($code);
  return substr($code, $len-1);
}

my %hashmap = &getCourseList;
#my …
Run Code Online (Sandbox Code Playgroud)

perl cgi substring

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

标签 统计

c++ ×3

c ×1

cgi ×1

gcc ×1

icarus ×1

oop ×1

perl ×1

pointers ×1

reference ×1

string ×1

substring ×1

this ×1

verilog ×1