'class String' has no member named 'write'

Hi, all

I have a question with String and Servo.h.

First, I define a array , and its shape is [6][3].

and then, I run this part:

void motor_attach(){
** for (int i=0;i<=5;i++){**
** for (int j=0;j<=2;j++){**
Servo motor_name*[j];
motor_name[j].attach(motor_pin[j]);}}}[/b]
_ it’ OK, but When I run the next part:
void initial_state(){
** for (int i=0; i<=5;i++){
for(int j=0;j<=2;j++){**_

motor_name[j].write(motor_angle[j]);
_ }}}[/b]
The system tell me ‘class String’ has no member named ‘write’
I don’t know how to solve this question*

* Hope someone can me, Thank you*_

Please post your full sketch.

If possible, you should always post code directly in the forum thread as text using code tags:

  • Do an Auto Format (Tools > Auto Format in the Arduino IDE or Ctrl + B in the Arduino Web Editor) on your code. This will make it easier for you to spot bugs and make it easier for us to read.
  • In the Arduino IDE or Arduino Web Editor, click on the window that contains your sketch code.
  • Press “Ctrl + A”. This will select all the text.
  • Press “Ctrl + C”. This will copy the selected text to the clipboard.
  • In a forum reply here, click the “Reply” button.
  • click on the reply field.
  • Click the </> button on the forum toolbar. This will add the forum’s code tags markup to your reply.
  • Press “Ctrl + V”. This will paste the sketch between the code tags.
  • Move the cursor outside of the code tags before you add any additional text to your reply.
  • Repeat the above process if your sketch has multiple tabs.

This will make it easy for anyone to look at it, which will increase the likelihood of you getting help.

If the sketch is longer than the 9000 characters maximum allowed by the forum, then it’s OK to add it as an attachment. After clicking the “Reply” button, you will see an “Attachments and other settings” link that will allow you to make the attachment.

When your code requires a library that’s not included with the Arduino IDE please post a link (using the chain links icon on the forum toolbar to make it clickable) to where you downloaded that library from or if you installed it using Library Manger (Sketch > Include Library > Manage Libraries in the Arduino IDE or Libraries > Library Manager in the Arduino Web Editor) then say so and state the full name of the library.

const int foot_num = 6;
const int joint_num = 3;
String motor_name[foot_num][joint_num] = {{"LFC", "LFF", "LFT"},
  {"LMC", "LMF", "LMT"},
  {"LRC", "LRF", "LRT"},
  {"RFC", "RFF", "RFT"},
  {"RMC", "RMF", "RMT"},
  {"RRC", "RRF", "RRT"}
};
int motor_pin[foot_num][joint_num] = {{3 ,  4,  5},   // LF
  {6 ,  7,  8},   // LM
  {9 , 10, 11},   // LR
  {12, 13, 14},   // RF
  {15, 16, 17},   // RM
  {18, 19, 20}
};  // RR
float motor_angle[foot_num][joint_num] = {{0,  90, 120},  // LF
  {90, 90, 120},  // LM
  {165, 90, 120}, // LR
  {0  , 90, 120}, // RF
  {90 , 90, 120}, // RM
  {105, 90, 120}
}; // RR
// ==================== Motor Define & Connect ========================
void motor_attach() {
  for (int i = 0; i <= 5; i++) {
    for (int j = 0; j <= 2; j++) {
      Servo motor_name[i][j];                     // Define  Servo motor
      motor_name[i][j].attach(motor_pin[i][j]);
    }
  } // Connect Servo motor
}
// ========================= Initial State ============================
void initial_state() {
  for (int i = 0; i <= 5; i++) {
    for (int j = 0; j <= 2; j++) {
      motor_name[i][j].write(motor_angle[i][j]);
    }
  }
}
String motor_name[foot_num][joint_num] = {{"LFC", "LFF", "LFT"},

Here you declare an array of Strings with global scope.

Servo motor_name[i][j];                     // Define  Servo motor

Here you are creating an array of Servo objects with local scope to the motor_attach function. C++ allows you to have multiple variables of the same name at different scopes. This is known as variable shadowing. It can be confusing to figure out which variable is being used when there are multiple with the same name.

motor_name[i][j].write(motor_angle[i][j]);

Since this is outside the scope of the motor_attach function, you are referencing the array of Strings named motor_attach. The String class has no write() function, thus the error.

I would recommend not using multiple variables with the same name. Surely you can think of different appropriate names for each variable. That will make things less confusing, but it won't solve the error. Even if you rename the array of Servo objects, you still won't be able to use it outside its scope, you'll just get a more meaningful error message. If you want to use a variable in multiple functions, you either need to declare the variable in the global scope or else you need to pass the variable between the functions. Since you didn't post your full sketch, it's difficult to provide detailed assistance, but once you understand the problem you should be able to find the solution on your own.