# Generating an object within a class

**URL:** <https://forum.arduino.cc/t/generating-an-object-within-a-class/1460685>\
**Category:** Programming\
**Created:** [September 23, 2026, 2:12pm UTC](https://forum.arduino.cc/t/generating-an-object-within-a-class/1460685 "2026-09-23T14:12:44Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![SupArdu](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/supardu/32/1089208_2.png) [@SupArdu](https://forum.arduino.cc/u/SupArdu)\
**Post date:** [September 23, 2026, 2:12pm UTC](https://forum.arduino.cc/t/generating-an-object-within-a-class/1460685/1 "2026-09-23T14:12:44Z")

</div>

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

```arduino
#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:

```arduino
#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

```arduino
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.

---

<div class="post-metadata">

**Author:** ![robtillaart](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/robtillaart/32/1098811_2.png) [@robtillaart](https://forum.arduino.cc/u/robtillaart)\
**Post date:** [September 23, 2026, 2:26pm UTC](https://forum.arduino.cc/t/generating-an-object-within-a-class/1460685/2 "2026-09-23T14:26:40Z")

</div>

Try change the #define cspin into

Const int cspin = 11;

---

<div class="post-metadata">

**Author:** ![robtillaart](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/robtillaart/32/1098811_2.png) [@robtillaart](https://forum.arduino.cc/u/robtillaart)\
**Post date:** [September 23, 2026, 2:28pm UTC](https://forum.arduino.cc/t/generating-an-object-within-a-class/1460685/3 "2026-09-23T14:28:37Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![sterretje](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/sterretje/32/391294_2.png) [@sterretje](https://forum.arduino.cc/u/sterretje)\
**Post date:** [September 23, 2026, 2:41pm UTC](https://forum.arduino.cc/t/generating-an-object-within-a-class/1460685/4 "2026-09-23T14:41:35Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![GolamMostafa](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/golammostafa/32/239011_2.png) [@GolamMostafa](https://forum.arduino.cc/u/GolamMostafa)\
**Post date:** [September 23, 2026, 6:52pm UTC](https://forum.arduino.cc/t/generating-an-object-within-a-class/1460685/5 "2026-09-23T18:52:04Z")

</div>

> [@SupArdu](#):
>
> 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.

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

 ![image](https://europe1.discourse-cdn.com/arduino/original/4X/e/f/e/efecca0cc515e5326adc60c109493d317a53cdff.jpeg)  
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](https://github.com/RobTillaart/AD9833) @Robtillaart, which is simpler compare to `MD_AD9833.h` library.

---

<div class="post-metadata">

**Author:** ![kenb4](https://avatars.discourse-cdn.com/v4/letter/k/bb73d2/32.png) [@kenb4](https://forum.arduino.cc/u/kenb4)\
**Post date:** [September 23, 2026, 8:55pm UTC](https://forum.arduino.cc/t/generating-an-object-within-a-class/1460685/6 "2026-09-23T20:55:25Z")

</div>

Ah, this is the "Most Vexing Parse"

```cpp
#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.

---

<div class="post-metadata">

**Author:** ![SupArdu](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/supardu/32/1089208_2.png) [@SupArdu](https://forum.arduino.cc/u/SupArdu)\
**Post date:** [September 23, 2026, 9:34pm UTC](https://forum.arduino.cc/t/generating-an-object-within-a-class/1460685/7 "2026-09-23T21:34:24Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![gfvalvo](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gfvalvo/32/1013044_2.png) [@gfvalvo](https://forum.arduino.cc/u/gfvalvo)\
**Post date:** [September 25, 2026, 3:58pm UTC](https://forum.arduino.cc/t/generating-an-object-within-a-class/1460685/8 "2026-09-25T15:58:57Z")

</div>

> [@SupArdu](#):
>
> ```arduino
> class SigGen { // Signalgenerator
> public:
> ...
> private:
> uint8_t wave, onoff;
> MD_AD9833	*ptAD9833; // Hardware SPI
> MD_AD9833 AD(CS_AD9833);
> 
> ```

Since the `MD_AD9833` constructor takes an argument, you must initialize it in an [Initializer List](https://www.geeksforgeeks.org/cpp/when-do-we-use-initializer-list-in-c/):

```arduino
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.
