<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[C++ syntax question - Defining an object in a class that requires arguments?]]></title><description><![CDATA[<p dir="auto">I need some help with how to properly define an object in a class as a public object of the class for objects that require arguments.  I would like to hardcode the arguments.  For example I want to make the second UART port in a class and hardcode the UART to 2.</p>
<p dir="auto"><strong>.h file for class</strong></p>
<pre><code>#ifndef TestClass_h
#define TestClass_h

#include "Arduino.h"
#include &lt;M5Unified.h&gt;

class TestClass {
  public:
    //-- Define RS458 to UART to UART2, HardwareSerial needs a UART number passed to it --

    //What I have tried, I would like to hardcode the UART number to 2. How do I do this?????
    HardwareSerial SerialRS485; // 1 - Does not compile, I need something in the .cpp file to make this work
    HardwareSerial SerialRS485(uint8_t _uart); // 2 - Can get this to compile but then does not compile if I use a uncomment the test funciton
    HardwareSerial SerialRS485(uint8_t 2); // 3 - Does not compile

    //constructor
    TestClass();
      
    //Functions - Main
    void testFunction();


  private:
    uint16_t _private1 = 0;

};

#endif
</code></pre>
<p dir="auto"><strong>.cpp file for class</strong></p>
<pre><code>#include "TestClass.h"
#include "Arduino.h"

//Constructor def
TestClass::TestClass() {
}

void TestClass::testFunction() {
    Serial.println("USB Serial Port");
    SerialRS485.println("RS485 Serial Port");
}
</code></pre>
<p dir="auto"><strong>main ino file</strong></p>
<pre><code>#include "Arduino.h"
#include &lt;M5Unified.h&gt; //For M5Stack ESP Hardware

//Create test class
#include "TestClass.h"
TestClass TestObject;

void setup(void)
{
  auto cfg = M5.config();  //M5 Unified config
  M5.begin(cfg);
  delay(500);
}

void loop(void)
{
  M5.update(); //Update button state
  TestObject.testFunction();
  delay(1000);
}
</code></pre>
]]></description><link>https://community.m5stack.com/topic/6157/c-syntax-question-defining-an-object-in-a-class-that-requires-arguments</link><generator>RSS for Node</generator><lastBuildDate>Tue, 10 Mar 2026 09:38:30 GMT</lastBuildDate><atom:link href="https://community.m5stack.com/topic/6157.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 26 Feb 2024 17:01:14 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to C++ syntax question - Defining an object in a class that requires arguments? on Wed, 06 Mar 2024 15:03:49 GMT]]></title><description><![CDATA[<p dir="auto"><a class="mention plugin-mentions-user plugin-mentions-a" href="https://community.m5stack.com/uid/62789">@lukes</a> said in <a href="/post/24156">C++ syntax question - Defining an object in a class that requires arguments?</a>:</p>
<blockquote>
<p dir="auto">HardwareSerial</p>
</blockquote>
<p dir="auto">Maybe this solves your problem ...</p>
<pre><code>class StaticClass {
private:
    int myContent;
public:
    StaticClass(int v){myContent = v;}

    void printout(){printf("%d\n",myContent);}
};
    

class TestClass {

public:
    //StaticClass myClass = StaticClass(42);    // With C11
    StaticClass myClass;
        
    TestClass() : myClass(42)
    {
    }
        
    void doit()
    {
        myClass.printout();
    }
};


int main(void)
{
    TestClass tc;
    
    tc.doit();
}
</code></pre>
]]></description><link>https://community.m5stack.com/post/24272</link><guid isPermaLink="true">https://community.m5stack.com/post/24272</guid><dc:creator><![CDATA[pstoehr]]></dc:creator><pubDate>Wed, 06 Mar 2024 15:03:49 GMT</pubDate></item></channel></rss>