Help w/ code error

Pages: 12
I keep getting the error "expected declaration before '}' token" and I don't know what to do to fix it.

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
#include "webcount.h"




void WebCounter::display() 
{

cout << "number:  " << counter << endl;
}  

void WebCounter::set(int n)
{ 
  counter = n;
}

void WebCounter::hit()
{
  counter++;
}

void WebCounter::reset()
{
  counter = 0;
}

int  WebCounter::get()
{
   return counter;
}
What line number? Copy and paste the exact error message.
Oops. turns out the error is actually in my webcount.h file

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#ifndef WEBCOUNTER_H
#define WEBCOUNTER_H
#include <iostream>
using namespace std;


class  name {
    public:
           void display(); 
           void set(int n);
           void hit();
           void reset();
           int get();

         private:
           int counter;

};
}

#endif 


Exact message is "webcount.h:19:1 error: expected declaration before '}' token"
You've got an extraneous closing curly brace on line 19 - the indentation should have tipped you off, and the error should have hit the nail on the head. Now you know ;)
So I just delete the curly brace in line 19?
Now that I did that I am getting the error ".text+0x18): undefined reference to 'main'
collect2: error: ldd returned 1 exit status" whenever I try to compile my WebCounter.cpp using the g++ command.

Here are my two codes

WebCounter.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
30
#include "WebCounter.h"
#include <iostream>



void Webcounter::display() 
{

cout << "number:  " << counter << endl;
}  

void Webcounter::set(int n)
{ 
  counter = n;
}

void Webcounter::hit()
{
  counter++;
}

void Webcounter::reset()
{
  counter = 0;
}

int  Webcounter::get()
{
   return counter;
}


WebCounter.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#ifndef WEBCOUNTER_H
#define WEBCOUNTER_H
#include <iostream>
using namespace std;


class  name {
    public:
           void display(); 
           void set(int n);
           void hit();
           void reset();
           int get();

         private:
           int counter;

};


#endif 
Do you have a source file with a main function in it for this program? Are you compiling and linking to it properly (I would imagine not)?

Try, for simplicity's sake, adding main to your WebCounter.cpp file and see if that fixes it.
Okay I deleted the extraneous "}" in the .h file and a bunch of errors come up that says my Webcounter has not been declared in lines 5,6,11,12,16,17,21,22,26,27.
Oh my God, I can't believe I missed this: the class is called name, but your functions refer to class Webcounter. Change the name of the class to Webcounter.
OKay that fixed that error, butttt I got another few.

In lines 7,12,17,22,27, it says a function definition is not allowed here before '{' token in my WebCounter.cpp file

EDIT: fixed that, now it is saying in lines 14:12, error: 'n' was not declared in this scope.
Last edited on
Compiles for me. This makes me think that you are leaving something out. Do you have any other files for this program?
Just the WebCounter.h and the WebCounter.cpp

I might have done something to screw it up... here are the codes again

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
#include "WebCounter.h"
#include <iostream>


void WebCounter::display() 

{
cout << "Number of hits:   " << counter << endl;
}  

void WebCounter::set(int number)

{ 
 counter = n;
}

void WebCounter::hit()

{
 counter++;
}

void WebCounter::reset()

{
 counter = 0;
}

int  WebCounter::get()

{
 return counter;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#ifndef WEBCOUNTER_H
#define WEBCOUNTER_H
#include <iostream>
using namespace std;


class  name {
    public:
           void display(); 
           void set(int n);
           void hit();
           void reset();
           int get();

         private:
           int counter;

};


#endif 
Last edited on
So you don't have the main function anywhere (back to an earlier post)?

I copy and pasted the code, changed the name of the class and compiled with no problems:

>gcc -c Webcounter.cpp

>


Can you provide the updated codes? Also, can you provide the command you are using to compile?
Updated Codes

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
#include "WebCounter.h"
#include <iostream>


void WebCount::display() 

{
cout << "Number of hits:   " << counter << endl;
}  

void WebCount::set(int number)

{ 
 counter = n;
}

void WebCount::hit()

{
 counter++;
}

void WebCount::reset()

{
 counter = 0;
}

int  WebCount::get()

{
 return counter;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#ifndef WEBCOUNTER_H
#define WEBCOUNTER_H
#include <iostream>
using namespace std;


class  WebCount {
    public:
           void display(); 
           void set(int n);
           void hit();
           void reset();
           int get();

         private:
           int counter;

};


#endif 


I am using >g++ WebCounter.cpp -o Webcounter
I believe the -o switch is used for linking, which won't work because you don't have a main function. By adding a main function to the Webcounter.cpp file, I can compile with no issues:

>g++ -o Webcounter Webcounter.cpp

>
So I would add int main () to the beginning of the code?
Yes, try adding it to the Webcounter.cpp file.
> I believe the -o switch is used for linking
nope, it defines the output file, whatever the stage it is.

> 14:12, error: 'n' was not declared in this scope.
1
2
3
4
5
void WebCounter::set(int number)

{ 
 counter = n;
}
¿where did you define what `n' is?
I never did define it..... how do i define?
In addition to Danny's great advice:

As i said in your other post, remove lines 3 and 4 from your header file. They aren't necessary for that particular file. There is nothing wrong with including <iostream> in the header file, may be it is my personal preference to include files where they are needed, rather than in another header file.

Line 4 is a bad habit (best to break out of it early). Instead put std:: before each std thing. So in your .cpp file have std::cout, and std::endl

Would it be better to have a main.cpp file to create a Webcounter object in, and use it's interface from there?

As already mentioned by Danny, make you class name exactly the same as the files it is in. That is Webcounter

It is also a good idea to use a high level of warnings when compiling. I routinely use -Wall -Wextra -pedantic. You could also use -Werror to promote all warnings to errors. Have a look at your compiler documentation - there are some warnings that still aren't enabled by those mentioned, and you might find these useful when compiling from the shell as opposed to an IDE: -Wmissing-include-dirs, -Wswitch-default, -Wuninitialized, -Wfloat-equal, -Wconversion

I don't use these routinely because, either my IDE warns me in the editor, or I know never to code that way - for example FP equality .

Hope all is well, and this info was helpful. :+)
Pages: 12