identifier not found

Hi,

trying to split a function between a .cpp/.h and accessing it in main by calling that function. I keep getting the same error "identifier not found" though I can't figure out why and need some fresh eyes to look at this:

main.cpp
1
2
3
4
5
6
int main()
{
int x = 0;
passaval(x);
std::cout << "Value passed is:"<< passaval<< std::endl;
}



.cpp:
1
2
3
4
5
6
#include "passaval.h"

void passaval(int &x)
{
      x = 99;
}

.h:

1
2
3
4
#ifndef PASSAVAL_H    
#define PASSAVAL_H
void passaval(int x);
#endif 


 was expecting a 99

error above
Thx in advance
Last edited on
One obvious problem is that in your .cpp file, you're passing x by reference. In your .h file, you're saying x is passed by value. They must agree.
You also don't have a variable named passaval defined in main(), passaval() is a function returning void not a variable to be printed.

Also, you need to include passaval.h in main.cpp.
Adjusted. Thank you.

main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13

#include "pch.h"
#include <iostream>
#include "passaval.h"

int main()
{
    std::cout << "Hello World!\n";
	int x;
	passaval(x);
	std::cout << x << std::endl;
	
}

.cpp:
1
2
3
4
5
6
7
8
9
#include "pch.h"
#include "passaval.h"

void passaval(int &x)
{
	
	x = 99;

}

.h:
1
2
3
4
5
6
7
#pragma once
#ifndef PASSAVAL_H    
#define PASSAVAL_H

void passaval(int &x);

#endif  



Hello World!
99
Registered users can post here. Sign in or register to post.