Notifications
Clear all

Constructor Error

31 Posts
4 Users
8 Likes
1,412 Views
barrie
(@barrie)
Member
Joined: 2 years ago
Posts: 86
Topic starter  
Trying to write a function but can't figure out what is wrong. Both arguments were declared as integers prior to the function.
I get this error:-
Compilation error: expected constructor, destructor, or type conversion before '(' token
 The error highlights the first line of code.
```cpp
// function to change speed
newSpeed (stepSpeed1, stepSpeed2)
{
stepper1.setSpeed(stepSpeed1);
stepper2.setSpeed(stepSpeed2);
}
```

   
Quote
Inq
 Inq
(@inq)
Member
Joined: 2 years ago
Posts: 1900
 

If this is a plain C function, it needs a return type... if that is the whole function, it probably needs to be

void newSpeed (stepSpeed1, stepSpeed2)
{
    stepper1.setSpeed(stepSpeed1);
    stepper2.setSpeed(stepSpeed2);
}

 

If it is a c++ member function it must also  have class name in front of it...

void yourClass::newSpeed (stepSpeed1, stepSpeed2)
{
    stepper1.setSpeed(stepSpeed1);
    stepper2.setSpeed(stepSpeed2);
}

 

3 lines of code = InqPortal = Complete IoT, App, Web Server w/ GUI Admin Client, WiFi Manager, Drag & Drop File Manager, OTA, Performance Metrics, Web Socket Comms, Easy App API, All running on ESP8266...
Even usable on ESP-01S - Quickest Start Guide


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6662
 

Please read the Help about how to post code. Also, do a screen grab ot copy paste of the actual errors, do NOT try to type it in yourself. Sometimes it helps to scroll back thru the output listing to see if there is any more lines in RED. Of course you have set prefs to Compiler Warnings ALL.

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote
barrie
(@barrie)
Member
Joined: 2 years ago
Posts: 86
Topic starter  

I copied the code with "Copy for Forum" and pasted it into the text field. I simply copied and pasted the error message and manually highlighted it. I am using Arduino 2.0.0-9.4 where "Copy for Forum" is only accessed from the edit menu. Am I doing something wrong?  


   
ReplyQuote
barrie
(@barrie)
Member
Joined: 2 years ago
Posts: 86
Topic starter  

@inq I copied your plain C code and got this error. Compilation error: variable or field 'newSpeed' declared void

Removing "void" gave this error: "Compilation error: expected constructor, destructor, or type conversion before '(' token"

I didn't try the C++ because I don't understand theC++  class system.

When I remove the function, all code compiles OK. 

I didn't have a problem with functions that have no return value.

This post was modified 2 years ago by barrie

   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6662
 

@barrie It's confusing, but for the forum it's better to use the other option 'Copy as HTML' you can see an example of that in the preceding post https://forum.dronebotworkshop.com/postid/33743 /" target="_blank" rel="noopener"> https://forum.dronebotworkshop.com/postid/33743/

I think that post also has the answer to your query.

I tried that code snippet and was greeted with the following

 

ketch_sep14a:10:10: error: expected constructor, destructor, or type conversion before '(' token
newSpeed (stepSpeed1, stepSpeed2)
^
sketch_sep14a:10:10: error: expected constructor, destructor, or type conversion before '(' token
newSpeed (stepSpeed1, stepSpeed2)
^
exit status 1
expected constructor, destructor, or type conversion before '(' token

 

Notice the ^ missing in yours. 

It's not enough info to just copy the one line, the surrounding lines are generally more informative.

@Inq has translated the error message into the actual form required. However uncharacteristically he missed a couple other errors, namely the types for the two stepSpeedx variables and the declaration for stepper1 and stepper2.

I am not C++ savvy, so I might have overlooked something, but I think we got it all.

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
Inq reacted
ReplyQuote
Inq
 Inq
(@inq)
Member
Joined: 2 years ago
Posts: 1900
 

@barrie - @zander is right, I missed more obvious things.

  1. All functions MUST have return types.  If you don't want to return something to the calling code, you must prefix it with void.
  2. All parameters must have a variable type.  As the stepper.h library functions void setSpeed(long whatSpeed); use a long, you need to pass in that long...
void newSpeed (long stepSpeed1, long stepSpeed2)
{
    stepper1.setSpeed(stepSpeed1);
    stepper2.setSpeed(stepSpeed2);
}

 

3 lines of code = InqPortal = Complete IoT, App, Web Server w/ GUI Admin Client, WiFi Manager, Drag & Drop File Manager, OTA, Performance Metrics, Web Socket Comms, Easy App API, All running on ESP8266...
Even usable on ESP-01S - Quickest Start Guide


   
frogandtoad reacted
ReplyQuote
barrie
(@barrie)
Member
Joined: 2 years ago
Posts: 86
Topic starter  

Oh dear! version 2.0 does not have 'Copy as HTML'. I Sorry, I don't understand the relevance of '^'.

I will try to post the entire code using version 1.8.19.


   
ReplyQuote
Inq
 Inq
(@inq)
Member
Joined: 2 years ago
Posts: 1900
 
Posted by: @barrie

Oh dear! version 2.0 does not have 'Copy as HTML'. I Sorry, I don't understand the relevance of '^'.

I will try to post the entire code using version 1.8.19.

No, you don't need to do that.

Simply highlight all the text you want to include from whatever editor 2.0, 1.8.19, any other editor.  I use Notepad++ for instance.

In this forum's editor, simply click the <> button where you want to add source code.

Paste it in the popup window... Done.

image

3 lines of code = InqPortal = Complete IoT, App, Web Server w/ GUI Admin Client, WiFi Manager, Drag & Drop File Manager, OTA, Performance Metrics, Web Socket Comms, Easy App API, All running on ESP8266...
Even usable on ESP-01S - Quickest Start Guide


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6662
 

@inq Also missing declares for stepper1 and stepper2 but they may be in the global section

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
frogandtoad reacted
ReplyQuote
barrie
(@barrie)
Member
Joined: 2 years ago
Posts: 86
Topic starter  

@inq I declared the stepspeed variables as integers in the spaces above Void setup(). I changed them to long but still got error. I will post all the code and hopefully you will find it easier to spot my error.


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6662
 

@barrie You have to also specify the type for the parameters as in

void newSpeed(long stepSpeed1, long stepSpeed2)

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
frogandtoad reacted
ReplyQuote
barrie
(@barrie)
Member
Joined: 2 years ago
Posts: 86
Topic starter  

Should this have the keywords highlighted?

/*
Two 28BYJ-48 5V Stepper motors controlled by HC-SR04 Sonar module
*/
 
//--------------------------Stepper motor globals-----------------------
#include <AccelStepper.h>
 
// Define Constants
//distance x 3.3 = speed
const int speedFacter = 3.3;
// Define step constants
int maxSpeed = 200;
int lowSpeed = 50;
#define FULLSTEP 4
#define HALFSTEP 8
// define variables
long stepSpeed1 = maxSpeed;
long stepSpeed2 = maxSpeed;
unsigned long startTime;
unsigned long elapsedTime;
// Define Motor Pins (2 Motors used)
 
#define motorPin1 8 // Blue - 28BYJ48 pin 1
#define motorPin2 9 // Pink - 28BYJ48 pin 2
#define motorPin3 10 // Yellow - 28BYJ48 pin 3
#define motorPin4 11 // Orange - 28BYJ48 pin 4
 
 
#define motorPin5 4 // Blue - 28BYJ48 pin 1
#define motorPin6 5 // Pink - 28BYJ48 pin 2
#define motorPin7 6 // Yellow - 28BYJ48 pin 3
#define motorPin8 7 // Orange - 28BYJ48 pin 4
 
// Define two motor objects
// The sequence 1-3-2-4 is required for proper sequencing of 28BYJ48
AccelStepper stepper1(HALFSTEP, motorPin1, motorPin3, motorPin2, motorPin4);
// AccelStepper stepper2(FULLSTEP, motorPin5, motorPin7, motorPin6, motorPin8);
AccelStepper stepper2(HALFSTEP, motorPin5, motorPin7, motorPin6, motorPin8);
//--------------------------Sonar-HC-SR04 globals-----------------------------------------
// Hook up HC-SR04 with Trig to Arduino Pin 10, Echo to Arduino pin 13
// Maximum Distance is 400 cm
// Include the AccelStepper Library
// Include NewPing Library
#include "NewPing.h"
#define TRIGGER_PIN 12
#define ECHO_PIN 13
#define MAX_DISTANCE 400
 
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
 
float distance;
//-------------------------------------------------------------------------
void setup()
// stepper setup
{
// 1 revolution Motor 1 CW
stepper1.setMaxSpeed(1000.0);
stepper1.setAcceleration(50.0);
stepper1.setSpeed(stepSpeed1);
stepper1.moveTo(2048);
 
// 1 revolution Motor 2 CCW
stepper2.setMaxSpeed(1000.0);
stepper2.setAcceleration(50.0);
stepper2.setSpeed(stepSpeed2);
stepper2.moveTo(-2048);
 
//-------------------------Sonar setup------------------------------------
// define pins 14, 15 and 16 for LED distance indicators
for (int i = 14; i < 17; i++) {
pinMode (i,OUTPUT);
}
}

//define function to turn LED's off
void alloff(void) {
for (int i = 14; i < 17; i++){
digitalWrite (14,LOW);
}
}
//define function to turn LED's on
void allon(void) {
for (int i = 14; i < 17; i++){
digitalWrite (14,HIGH);
}
}
//define function to Change direction at the limits
void changDirection(void) {
if (stepper1.distanceToGo() == 0)
stepper1.moveTo(-stepper1.currentPosition());
if (stepper2.distanceToGo() == 0)
stepper2.moveTo(-stepper2.currentPosition());
}

// function to change speed
newSpeed (stepSpeed1, stepSpeed2)
{
stepper1.setSpeed(stepSpeed1);
stepper2.setSpeed(stepSpeed2);
}
// =============================================
void loop()
{
distance = sonar.ping_cm();
int (d) = distance;
switch (d){
//change direction
case 2 ... 4:
alloff;
allon;
digitalWrite (14,HIGH);
digitalWrite (15,HIGH);
changDirection();
break;
case 5 ... 10:
alloff;
digitalWrite(14,HIGH);
alloff();
digitalWrite(14, HIGH);
stepSpeed1 = lowSpeed;
stepSpeed2 = lowSpeed;
//newSpeed (stepSpeed1, stepSpeed2);
break;
case 11 ... 15:
alloff();
digitalWrite(15, HIGH);
stepSpeed1 = maxSpeed/2;
stepSpeed2 = maxSpeed/2;
//newSpeed (stepSpeed1, stepSpeed2);
break;
case 16 ... 20:
alloff();
digitalWrite(16, HIGH);
stepSpeed1 = maxSpeed;
stepSpeed2 = maxSpeed;
//newSpeed (stepSpeed1, stepSpeed2);
break;
//change stepper1 direction and speed
case 21 ... 30:
alloff();
allon ();
delay (1000);
alloff();
delay (1000);
allon ();

break;
case 31 ... 60:
// change speed with a conversion facter
stepSpeed1 = speedFacter * d;
stepSpeed2 = speedFacter * d;
alloff();
allon ();
default:
break;
}
/*Change direction at the limits
if (stepper1.distanceToGo() == 0)
stepper1.moveTo(-stepper1.currentPosition());
if (stepper2.distanceToGo() == 0)
stepper2.moveTo(-stepper2.currentPosition());
*/
 
stepper1.run();
stepper2.run();
 
}

   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6662
 

@barrie You did not follow the instructions, here they are

Step 1 – Copy the code sample from your computers editor, it can be any text editor or IDE.

Step 2 – Paste it into your post, at the point you want the sample to occur

Step 3 – To make things easier add a couple of line feeds after you paste it.

Step 4 – Highlight the code in your post.

Step 5 – Click the Code button (the one that looks like “<>”) on the post editor toolbar.

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote
barrie
(@barrie)
Member
Joined: 2 years ago
Posts: 86
Topic starter  

   
ReplyQuote
Page 1 / 3