Generating an object within a class

In my project I'm using the signal generator AD9833 with the library MD_AD9833. I'm struggling with generating an object to use AD9833.

There is an example code in the library

#include <MD_AD9833.h>
#include <SPI.h>

// Pins for SPI comm with the AD9833 IC
const uint8_t PIN_DATA = 11;  ///< SPI Data pin number
const uint8_t PIN_CLK = 13;  	///< SPI Clock pin number
const uint8_t PIN_FSYNC = 10; ///< SPI Load pin number (FSYNC in AD9833 usage)

MD_AD9833	AD(PIN_FSYNC);  // Hardware SPI

void setup(void)
{...

In this example the object AD is generated at the beginning of the sketch where also variables are generated. In my object I have a header file DeviceClasses.h with following (reduced) content:

#include "BaseClasses.h"
#include "MD_AD9833.h"    // Chip AD9833, wave form generator

#define SPI_SPEED  (4000000)
#define CS_AD9833  (11)
#define CS_AMPLT   (10)
...
class SigGen {    // Signalgenerator
public:
...
private:
  uint8_t wave, onoff;
  MD_AD9833	*ptAD9833;    // Hardware SPI
  MD_AD9833 AD(CS_AD9833);
  ...

I inserted the line before the last one as a test only, to show that the compiler accepts to generate a pointer to an object MD_AD9833.

On the last line (same method as in the example program) the compiler generates error messages like

include/DeviceClasses.h:11:20: error: expected identifier before '(' token
 #define CS_AD9833  (11)
                    ^
include/DeviceClasses.h:92:16: note: in expansion of macro 'CS_AD9833'
   MD_AD9833 AD(CS_AD9833);
                ^~~~~~~~~
include/DeviceClasses.h:11:21: error: expected ')' before numeric constant
 #define CS_AD9833  (11)
                     ^
include/DeviceClasses.h:92:16: note: in expansion of macro 'CS_AD9833'
   MD_AD9833 AD(CS_AD9833);
                ^~~~~~~~~
and a lot more

The #define seems to be a problem and also the generation of the instance AD of MD_AD9833.

Try change the #define cspin into

Const int cspin = 11;

A better way to initialize might be dependency injection. Create an ad9833 object outside the funcgen class and pass it as parameter in the constructor or so.

Your topic does not seem to indicate a problem with IDE 2.x and therefore has been moved to a more suitable location on the forum.

1 Are you using the following breakout board (Fig-1)? If yes, show the connection diagram between the breakout board and Arduino UNO.


Figure-1:

2. What kind of waveform do you want to generate using the AD9833 chip?
3. Run the MD_AD9833_Basic.ino sketch from the Library and check that you are getting the expected waveform.

4. Study the sketch of Step-3 and learn the methodology of developing program using C++.

Note: You can use this library @Robtillaart, which is simpler compare to MD_AD9833.h library.

Ah, this is the "Most Vexing Parse"

#define THREE (333)

String one(1);

struct Foo {
  String two(2);
  String three(THREE);
  String four{ 4 };
};

void setup() {}

void loop() {}

Unlike with one: inside a class/struct, two looks like you're declaring a function that returns a String and takes an integer. You certainly don't need to name your arguments, but you need the types; and the compiler -- for some reason -- allows you to "give an example" in this case.

With three and THREE, it's the same error, but with the macro confusing matters.

The fix is to use brace initialization, available since C++11, as shown with four. Do that consistently with all your object instantiation.

Thank you for your answers, especially the answer of kenb4 is "high C++ knowledge".

I tried:

Try change the #define cspin into

Const int cspin = 11;

but with compiler errors too.

I don' t use a breakout board but have my own board.

I decided to declare a ponter "MD_AD9833 *ptAD9833;" and use it in the rest of my sketch. This works.

Since the MD_AD9833 constructor takes an argument, you must initialize it in an Initializer List:

class SigGen {    // Signalgenerator
  public:
    SigGen : AD(CS_AD9833) {}
  private:
    uint8_t wave, onoff;
    MD_AD9833  *ptAD9833;    // Hardware SPI
    MD_AD9833 AD;

Unfortunately, since you didn't bother to post complete code, I have no way of test compilinng this. But that's the general idea.