Why is this wrong?

This code is for some reason wrong. I've gone over it 20 times and cannot figure it out.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include "second.h"
#include <iostream>
#include <string>
#include "main.h"
using namespace std;

 second::second() {
     second(string z) {//error line
     setName(z);
     }
 void setName(string x){
 name = x;
 }
        string getName(){
        return name;
        }
};

two::two(){
int b = 45;
cout << b;
}


Build messages:
||=== Build: Debug in Guide (compiler: GNU GCC Compiler) ===|
C:\Users\secret\Desktop\Guide\src\second.cpp||In constructor 'second::second()':|
C:\Users\secret\Desktop\Guide\src\second.cpp|8|error: expected primary-expression before '(' token|
C:\Users\secret\Desktop\Guide\src\second.cpp|8|error: expected primary-expression before 'z'|
C:\Users\secret\Desktop\Guide\src\second.cpp|8|error: expected ';' before '{' token|
C:\Users\secret\Desktop\Guide\src\second.cpp|11|error: a function-definition is not allowed here before '{' token|
C:\Users\secret\Desktop\Guide\src\second.cpp|14|error: a function-definition is not allowed here before '{' token|
||=== Build failed: 5 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
Dont name one of your h files main.

Edit: Also, it looks like you're creating a class. But where is your class? Why are you using };? Im a bit confused.
Last edited on
Your code with some better looking formatting:

1
2
3
4
5
6
7
8
9
10
11
second::second() {
	second(string z) {//error line
		setName(z);
	}
	void setName(string x){
		name = x;
	}
	string getName(){
		return name;
	}
};


So you are trying to define 3 functions inside the definition of another function. You cannot define a function inside another function definition.
Topic archived. No new replies allowed.