将元素添加到数组中时出错 [英] Error in adding element into an array

查看:64
本文介绍了将元素添加到数组中时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我正在做一个arduino项目来控制继电器开关,使用CapacitiveSensor



i发现我的阵列有一些错误。



不确定我的阵列设置有什么问题



请指教谢谢,



我还是c ++编程的新手



我尝试了什么:



< pre>  #include   <   CapacitiveSensor.h  >  

CapacitiveSensor _pianoTiles [] = {};

/ *
* pin2 Piezo's Ports
* pin3~pin6继电器开关的端口
* /


int _outputPorts [] = { 2 3 4 5 6 };

/ *
* pin10~13:capacitiveSensor的Ports
* /


int _inputPorts [] = { 10 ,< span class =code-digit> 11 , 12 13 };

void setup(){

// boundRate:
Serial.begin( 9600 );

for int inputPorts = 0 ; inputPorts< sizeof (_inputPorts) - 1 ; inputPorts ++)_ pianoTiles [inputPorts] = CapacitiveSensor( 9 ,_ inputPorts [inputPorts]);

for int outputPorts = 0 ; outputPorts< sizeof (_ outputPorts) - 1 ; outputPorts ++)pinMode (_outputPorts [outputPorts],OUTPUT);

}

void loop(){

int value [] = {};

for int i = 0 ; i< sizeof (_pianoTiles) - 1 ; i ++)< span class =code-keyword> value [i] = _pianoTiles [i] .capacitiveSensor( 30 );

if (_ pianoTiles [ 0 ]> 200 )digitalWrite( 2 1 );

}







此行有错误:

< pre> if(_pianoTiles [0]> 200)digitalWrite(2,1); 





错误消息:

 Arduino:1.8.3每小时建立2017/04/14 10:33(Windows 7),董事会:Arduino / Genuino Uno 

C:\ Users \racheal\Desktop\smchProject\smchProject.ino:在函数'void loop()'中:

smchProject:35:错误:'运算符>'不匹配(操作数类型为'CapacitiveSensor'和'int')

if(_pianoTiles [0]> 200)digitalWrite(2,1);

^

退出状态1
不匹配'运算符>'(操作数类型为'CapacitiveSensor'和'int')

此报告将提供更多信息,其中
在编译期间显示详细输出在文件中启用了
选项 - >首选项。

解决方案

错误消息非常明确:

不匹配'operator>'(操作数类型是'CapacitiveSensor'和'int')



所以看看代码:

 if(_pianoTiles [0]> 200)... 



_pianoTiles 被声明为 CapacitiveSensor 对象:

 CapacitiveSensor _pianoTiles [] = {}; 

200显然一个整数。

错误消息告诉你没有声明操作符允许系统分辨两种对象类型中的哪一种 CapacitiveSensor 整数是最大的。



定义运算符> for CapacitiveSensor 取整数,或者排除你要比较的 CapacitiveSensor 的哪一部分。


hello, I am doing a project of arduino to control the relay switch by using the CapacitiveSensor

i found that there is some error on my array.

not sure is there any problem in my setup for array

please advice thank you,

I am still new in c++ programming

What I have tried:

<pre>#include <CapacitiveSensor.h>

CapacitiveSensor _pianoTiles[] = {};

/*
   * pin2 Piezo's Ports
   * pin3 ~ pin6 Relay Switch's Ports
   */
   
  int _outputPorts[] = {2 , 3 , 4 , 5 , 6}; 

  /*
   * pin10 ~ 13 : capacitiveSensor's Ports
   */

   int _inputPorts[] = {10, 11, 12, 13};

void setup() {

  //boundRate:
  Serial.begin  (9600);

  for ( int inputPorts = 0 ; inputPorts < sizeof( _inputPorts ) - 1 ; inputPorts ++) _pianoTiles[inputPorts] = CapacitiveSensor( 9 , _inputPorts[inputPorts]);

  for ( int outputPorts = 0 ; outputPorts < sizeof( _outputPorts ) - 1 ; outputPorts ++ ) pinMode ( _outputPorts[outputPorts], OUTPUT);

}

void loop() {

  int value[]={};

  for ( int i = 0 ; i < sizeof( _pianoTiles ) - 1 ; i ++ ) value[i] = _pianoTiles[i].capacitiveSensor(30);

  if ( _pianoTiles[0] > 200 ) digitalWrite( 2 , 1 );

}




there is an error on this line :

<pre>if ( _pianoTiles[0] > 200 ) digitalWrite( 2 , 1 );



error message :

Arduino: 1.8.3 Hourly Build 2017/04/14 10:33 (Windows 7), Board: "Arduino/Genuino Uno"

C:\Users\racheal\Desktop\smchProject\smchProject.ino: In function 'void loop()':

smchProject:35: error: no match for 'operator>' (operand types are 'CapacitiveSensor' and 'int')

   if ( _pianoTiles[0] > 200 ) digitalWrite( 2 , 1 );

                       ^

exit status 1
no match for 'operator>' (operand types are 'CapacitiveSensor' and 'int')

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

解决方案

The error message is pretty explicit:

no match for 'operator>' (operand types are 'CapacitiveSensor' and 'int')


So look at the code:

if ( _pianoTiles[0] > 200 ) ...


_pianoTilesis declared as an array of CapacitiveSensor objects:

CapacitiveSensor _pianoTiles[] = {};

And 200 is obviously an integer.
The error message is telling you that there is not operator declared which allows the system to tell which of the two object types CapacitiveSensor and integer is the "biggest".

Either define a operator> for CapacitiveSensor that takes an integer, or sort out which part of the CapacitiveSensor you meant to compare.


这篇关于将元素添加到数组中时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆