Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 2 of 2

Thread: How do you make a class instance itself?

  1. #1
    Senior Member
    Join Date
    May 2015
    Posts
    381

    How do you make a class instance itself?

    I am trying to convert some code to a class and it mostly works except for one thing I want to add. Let me explain what I am trying to do by example. When you #include <Wire.h> it creates a Wire object of class of TwoWire. You don't have to have TwoWire Wire(i2c0_addr, TwoWire::i2c0_hardware); in your code just including the library is enough.

    What I have tried that didn't work is:

    Code:
    // TestCL.ino
    #include "TestCL.h"
    
    
    //TestCL SVM; <<<<<<<<<<<<<<<< uncomment this and it works
    
    void setup() {
      SVM.begin();
    }
    void loop() {
    }
    
    // TestCL.cpp
    #include "TestCL.h"
    void TestCL::begin() {
      Serial.println("Started");
    }
    TestCL SVM();  // <<<<<<< I thought this would work
    
    //TestCl.h
    #include <Arduino.h>
    class TestCL {
      public:
        void begin(void);
      private:
    
    };
    The error is
    Code:
    Arduino: 1.8.3 (Mac OS X), TD: 1.37, Board: "Teensy 3.2 / 3.1, Serial, 72 MHz, Faster, US English"
    
    TestCL: In function 'void setup()':
    TestCL:7: error: 'SVM' was not declared in this scope
       SVM.begin();
       ^
    'SVM' was not declared in this scope
    
    This report would have more information with
    "Show verbose output during compilation"
    option enabled in File -> Preferences.
    Anyone care to point out my error? I'm really new to the ++ part of C++ and I know its simple but I just don't get it.

  2. #2
    Senior Member
    Join Date
    May 2015
    Posts
    381
    I got an answer from the Arduino forum

    basically I needed in the .h file

    extern TestCL SVM;

    and in the .cpp file i needed:


    TestCL SVM = TestCL();

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •