Class/Object Help

#include <iostream>

using namespace std;

class HelloClass(){ //*Error: expected unqualified -id before ')' token
public:
void saying(){
cout <<"hi" <<endl;
}
};

int main(){
HelloClass() HelloObject;
HelloObject.saying;
return 0;
}








hello i am relatively new to c++ and lately i have been trying to tackle classes/objects. At line 5 it says i have an error. I have been continually having this problem with any basic class i try to create. If anybody could explain this, i would be greatly appreciative
Classes don't really expect () after their names like you have there. :) That means that lines 5 and 13 need changing.

However, at the same time, member functions do just like any other function. That means line 14 needs changing.

Good luck!

-Albatross
can you post what the code should look like? :)
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
class ai {
private:
    int row, collumn, returncordiate[2];
    //row, collumn
    char aimap[6][6]; 
    
    
public:
    
    ai(); 
    
    bool check(int i, int j){
        if(aimap[i][j] == 'S') return true;
        else return false; 
    }
    
    int passive1(){
        srand(time(0));
        returncordiate[0] = rand()%7;
        return returncordiate[0]; 
    }
    
    int passive2(){
        srand(time(0));
        returncordiate[1] = rand()%7; 
        return returncordiate[1];
    }
    
};

ai::ai(){
    std::ifstream ship_location;
    ship_location.exceptions(std::ifstream::failbit | std::ifstream::badbit);
    //open file
    try{
        ship_location.open("/Users/home/Desktop/battleship/battleship/ship.txt");
    }
    //catch error
    catch (std::ofstream::failure &e){
        std::cout << "An error has occured";
        ship_location.close(); 
    }
    //upload array
    for(int j = 0; j < 6; j++){
        for(int i = 0; i < 6; i++){
            ship_location >> aimap[j][i]; 
        }
    }
    ship_location.close(); 
}

some code i found on my computer. has a class with a few functions, a constructor, and private/public setup.
this is how you would declare the object.
ai boss;
Last edited on
Topic archived. No new replies allowed.