Classes and objects [ISO C++ forbids declaration of testa with no data type]].

Hey guys, I'm just starting to learn some c++, and I've encountered an error which seems puzzling to me. The issue I encounter is when trying to create multiple functions in a separate class file.

Example use in main, without a separate class file (works) :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
 #include <iostream>


using namespace std;


class test {

    public:
        void test1(){
            cout << "test1! \n";
    }
         void test2(){
            cout << "test2! \n";
    }
};

int main()
{
    cout << "Starting tests of class functions :" << endl;
    test t;
    t.test1();
    t.test2();

    return 0;

}

Adding a different class file with multiple functions (not working) :

Main.cpp :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include "cool.h"

using namespace std;


class test {

    public:
        void test1(){
            cout << "test1! \n";
    }
            void test2(){
            cout << "test2! \n";
    }
};

int main()
{
    cout << "Starting tests of class functions :" << endl;
    cool c;
    test t;
    t.test1();
    t.test2();
    c.testa();

    return 0;

}


Header file (cool.h)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
 #ifndef COOL_H
#define COOL_H

using namespace std;

class cool
{
    public:
         cool();
         testa();
    protected:
    private:
};

#endif // COOL_H 


Cool.cpp :
1
2
3
4
5
6
7
8
9
10
11
12
 #include <iostream>
#include "cool.h"


cool::cool()
        {
            cout << "something! \n";
        }
cool::testa()
    {
        cout << "testa!\n";
    }


Error (cool.h) line 10: ISO C++ forbids declaration of 'testa' with no type|
||=== Build finished: 1 errors, 0 warnings ===|


Edit : I've re-read my tutorial which made no comment on using multiple functions in a separate class file, and I've scanned the forums without finding a solution to this. I believe this is a very simple mistake, but I just can't seem to find where I went wrong.
Last edited on
The testa function must have a return type indicated. For example,

1
2
3
 public:
         cool();
         void testa();


Similarly in the definition of the testa function.
Last edited on
I tried this, and while it corrects the issue in the header file, a new error pops up in the class file.

Header changes on line 10 :
void testa();

Class file changes on line 9
cool::void testa()

Error on line 9 in class file : expected unqualified-id before 'void'.
Last edited on
cool::void testa()

void is not something existing in the cool namespace.

Try this:

void cool::testa()
Ah, that would make sense... Solved! tyvm moschops!
Topic archived. No new replies allowed.