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 (Stripped down example):

// 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
Arduino: 1.8.3 (Mac OS X), TD: 1.37, Board: “Teensy 3.2 / 3.1, Serial, 72 MHz, Faster, US Engl”

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.

To do it the way you have you would need an extern declaration for SVM.

Why don't you look at the source code for Wire and see how it is done? Or it might be easier to see looking at how HardwareSerial gives you the Serial object.

Way down at the bottom of Wire.h is this:

extern TwoWire Wire;

And way down at the bottom of Wire.cpp is this:

// Preinstantiate Objects //////////////////////////////////////////////////////

TwoWire Wire = TwoWire();

Thank you very much!

I was looking at the code for Wire as used on the Teensy 3.2 and well I kind of got lost to tell the truth.

I

Yeah that's why I was saying it might be easier to look at HardwareSerial.

Jiggy-Ninja:
Way down at the bottom of Wire.h is this:

extern TwoWire Wire;

And way down at the bottom of Wire.cpp is this:

// Preinstantiate Objects //////////////////////////////////////////////////////

TwoWire Wire = TwoWire();

In this case, do you know why when you put Wire1 the instance still workind and change the I2C channel?. I was looking into the Wire.h and twi.h and I never find anything what change the pinnout or something when you write “Wire1”. Please Help to underestand this :frowning:

ammc: In this case, do you know why when you put Wire1 the instance still workind and change the I2C channel?. I was looking into the Wire.h and twi.h and I never find anything what change the pinnout or something when you write "Wire1". Please Help to underestand this :(

What board / processor?