Trouble using my own classes

I am new to C and C++ and I'm trying to get a project working using a class of my own, but I can't get classes to work so far. I think I might be missing something about prototyping or declaring the object or in calling the a function of the class.

I tried to run this code as a test:

class test;
test myTest;

void setup() {
  Serial.begin(9600);
}

void loop() {   
Serial.println(myTest::getString());
}

class test{

public:

 String str;

  test(){
  str = "sdfsdf"; 
  }

  test(String s){
  str = s;
  }

  void setString(String a){
  str = a;
  }

  String getString(){
  return str;
  }
  
};

but would get this error: aggregate 'test myTest' has incomplete type and cannot be defined -Highlighting line 2.

If I changed that line to: test myTest(); I instead get this error: 'myTest' is not a class or namespace -Highliting the Serial.println(myTest::getString()); section in my code.

Any Ideas on what I am doing wrong? Thanks in advance.

class test;

You have not defined the class. All you have done is said that there will be a class called test.

A class is a cookie-cutter. The definition gives it shape. You are simply saying, here, that test is going to be a cookie-cutter.

As far as that code goes, test is incomplete at the point you try and declare a variable of it.

Get rid of the forward declaration and put the class there instead (not at the end of sketch).

You also access members using '.' not '::' (the latter is for accessing static members from a class name, not a variable of).

oraz: Any Ideas on what I am doing wrong?

I'm guessing you want something like:

class test{

public:

 String str;

  test(){
  str = "sdfsdf"; 
  }

  test(String s){
  str = s;
  }

  void setString(String a){
  str = a;
  }

  String getString(){
  return str;
  }
  
};

test myTest;

void setup() {
  Serial.begin(9600);
}

void loop() {   
Serial.println(myTest.getString());
}

That worked :)

It was the prototyping that was confusing me. I also didn't realize that the syntax for accessing functions was the same as in java in that case and the double colon wasn't needed there. Thanks!