Help w/ code error

Pages: 12
Oct 3, 2013 at 5:32am
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;
}
Oct 3, 2013 at 5:37am
What line number? Copy and paste the exact error message.
Oct 3, 2013 at 5:42am
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"
Oct 3, 2013 at 5:45am
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 ;)
Oct 3, 2013 at 5:50am
So I just delete the curly brace in line 19?
Oct 3, 2013 at 6:36am
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 
Oct 3, 2013 at 6:41am
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.
Oct 3, 2013 at 6:46am
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.
Oct 3, 2013 at 6:50am
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.
Oct 3, 2013 at 7:02am
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 Oct 3, 2013 at 7:05am
Oct 3, 2013 at 7:05am
Compiles for me. This makes me think that you are leaving something out. Do you have any other files for this program?
Oct 3, 2013 at 7:10am
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 Oct 3, 2013 at 7:15am
Oct 3, 2013 at 7:15am
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?
Oct 3, 2013 at 7:19am
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
Oct 3, 2013 at 7:24am
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

>
Oct 3, 2013 at 7:26am
So I would add int main () to the beginning of the code?
Oct 3, 2013 at 7:33am
Yes, try adding it to the Webcounter.cpp file.
Oct 3, 2013 at 7:39am
> 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?
Oct 3, 2013 at 7:41am
I never did define it..... how do i define?
Oct 3, 2013 at 7:56am
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