Friend function not declared in this scope error - c++

Hi I am trying to understand the scope of friend functions and I get a "not declared in scope" error. Here is my code:
//node.h
class Node{
public:
int id;
int a;
int b;
friend int add(int,int);
void itsMyLife(int);
Node();
};
//node.cpp
Node::Node(){
a=0;
b=0;
id=1;
}
void Node::itsMyLife(int x){
cout<<"In object "<<id<<" add gives "<<add(x,a)<<endl;
}
//routing.cpp
#include "node.h"
int add(int x, int y){
return x+y;
}
//main.cpp
#include "node.h"
int main(){
return 0;
}
I get the error "add not declared in this scope" in node.cpp. Why do I get this error when I have declared the function in the class scope? Any help will be appreciated. Thanks

Inside your node class you declare a friend function int add (int, int). However, currently the compiler hasn't encountered the function yet and therefore it is unknown.
You could make a separate header and source file for your add function. Then in node.h include you new header. Because in the file where you declare Node the function add is not known currently.
So you might make a add.h and a add.cpp file for example and include add.h before declaring Node. Don't forget to compile add.cpp as well.

Its a bug on the the Linux side. The code should work. I have code right now that compiles fine on the Windows side and when I move it to the Linux side I get the same error. Apparently the compiler that you are using on the Linux side does not see/use the friend declaration in the header file and hence gives this error.
By simply moving the of the friend function's implementation in the C++ file BEFORE that function's usage (e.g.: as might be used in function callback assignment), this resolved my issue and should resolve yours also.
Best Regards

You haven't actually declared the function.
extern int add(int, int);

#include "node.h"
#include <iostream>
using namespace std;
class Node{
public:
int id;
int a;
int b;
friend int add(Node &a);
void itsMyLife(int);
Node();
};
//node.cpp
Node::Node(){
a=0;
b=0;
id=1;
}
void Node::itsMyLife(int x):b(x){
cout<<"In object "<<id<<" add gives "<<add(Node &a)<<endl;
}
//routing.cpp
#include "node.h"
int add(Node &a){
return a.b+a.y;
}
//main.cpp
int main(){
Node n;
n.ItsMyLife(15);
cout<<add(n);
return 0;
}
This should work fine - I guess. The syntax for "friend" function is -- friend {returntype} {functionname} (class_name &object_name). To access any of the members of the class use object_name.variable_name.

Related

Function not declared in scope / How use header.h, header.cpp, and main.cpp together?

I'm a complete beginner in C++ and I'm having trouble getting my functions declared in the header.h file, defined in header.cpp, and called in main.cpp. I've been looking for solutions but nothing I do seems to work. I would appreciate some input on how I can fix these issues.
Here's my code:
main.cpp
#include <iostream>
#include "DLinkedList.h"
int main() {
getInfo(); //Running my function getInfo
}
DLinkedList.h
#ifndef DLINKEDLIST_H
#define DLINKEDLIST_H
class Node
{
private:
int info;
public:
Node* prev;
Node* next;
int getInfo(); //Declaring my function getInfo
void setInfo(int value);
};
#endif
DLinkedList.cpp
#include <iostream>
#include "DLinkedList.h"
int getInfo() { //Defining my function getInfo
int answer;
std::cout << "Input the integer you want to store in the node: ";
std::cin >> answer;
return answer;
}
And the error message:
exit status 1
main.cpp: In function 'int main()':
main.cpp:6:3: error: 'getInfo' was not declared in this scope
getInfo();
^~~~~~~
getInfo() is not a free function. It is a member function of the class Node. So, it needs to be defined like this preceding with class name it belongs to using scope resolution operator i.e. :::
int Node::getInfo()
{
// ... body ...
}
And, in your main function, you need to create an object of your class before using its member function.
For example:
int main()
{
Node node;
node.getInfo();
return 0;
}
It's better to revise the OOP concepts and how they are implemented in C++ before writing code. Free functions and member functions are different things. Going through a proper book (or tutorial, etc.) would help you build the foundation for writing OOP code. Best of luck!

Various errors in a header file regarding a single type

I'm getting a number of errors in my header file that i can't seem to solve. They all seem to be in the lines that use the class Tutor type.
Here's my code:
#pragma once
#include "Pupil.h"
#include "Tutor.h"
class Class
{
char name;
int num;
Pupil** pupils;
int pupil_amount;
Tutor* tutor;
public:
Class();
Class(char, int);
~Class();
bool Add_Pupil(Pupil* p);
Pupil* Get_Pupil(int ind);
int Get_Amount()const { return pupil_amount; }//get the amount of pupils
int Get_Num()const { return num; }//get the name of the class
Tutor* Get_Tutor()const { return tutor; } //return a pointer to the tutor
void Add_Tutor(Tutor* t) { tutor = t; }//set a tutor recieved as a pointer
char Get_Name()const { return name; }
};
These are the errors:
I solved it by declaring the class "Tutor" as a friend but then my professor told me not to use friend declarations.
I tried moving the function to the .cpp file with no luck.
Is there any way i can solve this without using friend?
This error happens because compiler doesn't have declaration of 'Tutor' class when compiling your 'Class'. Check that 'Tutor.h' really contains declaration of the Tutor class.

C++ Linked Lists with struct

I'm new in C++ and I have something to do with a linked list, and I don't know why it doesn't work, need help from a prof :O)
Here's my .h
#ifndef UnCube_H
#define UnCube_H
using namespace std;
class ACube{
public:
ACube();
struct Thecube;
private:
void PrintList();
};
#endif
My ACube.cpp
#include <iostream>
#include "ACube.h"
ACube::ACube(){
};
struct Thecube{
int base;
int cube;
Thecube * next ;
};
void ACube::PrintList(){
};
and finally my main.cpp
#include <cstdlib>
#include <iostream>
#include "ACube.h"
using namespace std;
int main()
{
ACube * temp;
temp = (ACube*)malloc(sizeof(ACube));
for (int inc=1; inc <=20 ; inc++){
temp->ACube->nombrebase = inc;
temp->cube = inc*inc*inc;
}
system("PAUSE");
return EXIT_SUCCESS;
}
Everything was working fine, but when I add these lines :
temp->ACube->nombrebase = inc;
temp->cube = inc*inc*inc;
I add error saying :
'class ACube' has no member named 'TheCube'
'class ACube' has no member named 'cube'
Can someone help me because I want to create my list and fill the cube with number.
Other thing I want to use THIS. in the print,
Maybe someone can teach me what's wrong and how to do it !
Thanks for any help
You don't need to have a struct inside your class.
#ifndef UnCube_H
#define UnCube_H
using namespace std;
class ACube{
public:
ACube();
int base;
int cube;
ACube * next ;
private:
void PrintList();
};
#endif
ACube.cpp
#include <iostream>
#include "ACube.h"
ACube::ACube(){
};
void ACube::PrintList(){
};
Also, this string is wrong:
temp->ACube->nombrebase = inc;
it should be just:
temp->base = inc;
Last but not least, this code doesn't create a linked list, because you don't do anything with the ACube::next pointer.
There are so many horrible problems in your code, I suggest you should learn more C++ knowledge before writing linked list.
1. What is nombrebase?
I think nobody can answer.
2. You must allocate C++ class by new key word instead of malloc.
new invokes not only allocation but also class constructor, while malloc allocates only.
3. Thecube should been defined inside ACube
Since the code in your main() refers the member cube in class Thecube, main() must know what it is.
4. The member next in class ACube is a pointer which points to what?
What does a pointer point to without initilization? You should initial it in constructor, and destroy it in destructor.
5. temp->ACube
ACube is a class type, you can access member object, but not a type.
6. Never using namespace into a header file
It would make the client of header file has name collision.
The following is the corrected code. Just no compile error and runtime error, but this is NOT linked list:
ACube.h
#ifndef UnCube_H
#define UnCube_H
class ACube{
public:
struct Thecube
{
int base;
int cube;
Thecube * next;
};
ACube();
~ACube();
Thecube *next;
private:
void PrintList();
};
#endif
ACube.cpp
ACube::ACube()
: next(new Thecube)
{
}
ACube::~ACube()
{
delete next;
}
void ACube::PrintList(){
}
main.cpp
#include <cstdlib>
#include <iostream>
#include "ACube.h"
using namespace std;
int main()
{
ACube * temp;
temp = new ACube;
for (int inc = 1; inc <= 20; inc++)
{
temp->next->base = inc; // <-- This is not linked list, you shall modify.
temp->next->cube = inc*inc*inc; // <-- This is not linked list, you shall modify.
}
system("PAUSE");
return EXIT_SUCCESS;
}

I get an 'identifier not found' error

This is my first attempt to create a basic list (i need this at school) and i get a strange error.
This is the script:
#include "stdafx.h"
#include<iostream>
#include<conio.h>
using namespace std;
using namespace System;
using namespace System::Threading;
struct nod
{
int info;
nod *leg;
};
int n, info;
nod *v;
void main()
{
....
addToList(v, info); //I get the error here
showList(v); //and here
}
void addToList(nod*& v, int info)
{
nod *c = new nod;
c->info=info;
c->leg=v;
v=c;
}
void showList(nod* v)
{
nod *c = v;
while(c)
{
cout<<c->info<<" ";
c=c->leg;
}
}
The exact error is:
error C3861: 'addToList': identifier not found
I dont know why I get this... sorry if it is a stupid question but i am very new at this. Thanks for understanding.
you need to put a forward declaration to use a method before it's implementation. Put this before main :
void addToList(nod*& v, int info);
In C/C++ a method should be used only after it's declaration. To allow recursive call between different methods you can use forward declarations in order to allow the use of a function/method that will be forward implemented.
Identifiers must be declared before they are used. Move your declaration and definition of addToList earlier in the text file.
Thus:
#include "stdafx.h"
#include<iostream>
#include<conio.h>
using namespace std;
using namespace System;
using namespace System::Threading;
struct nod
{
int info;
nod *leg;
};
int n, info;
nod *v;
void addToList(nod*& v, int info)
{
nod *c = new nod;
c->info=info;
c->leg=v;
v=c;
}
void showList(nod* v)
{
nod *c = v;
while(c)
{
cout<<c->info<<" ";
c=c->leg;
}
}
void main()
{
....
addToList(v, info); //No more error here
showList(v); //and here
}
Try declaring addToList above main:
void addToList(nod*& v, int info);
Similarly for showList. The compiler needs to see a declaration of the function before it can use it.
Try placing the declarations of showList() and addToList() before main().

Constructor errors

I have this class header
//header for class.
#ifndef Container_H
#define Container_H
#include <iostream>
using namespace std;
const int DEFAULT=32;
class Container{
public:
Container(int maxCapacity = DEFAULT);
~Container();
void insert(int item, int index);
void erase(int index);
int size()const;
private:
int sizeC;
int capacityC;
int * elements;
};
void info();
#endif
and this source file
#include "container.h"
Container::Container(int maxCapacity = DEFAULT){
int y;
}
void Container::insert(int item, int index){
int x;
}
and when I compile this, I get the following error message
test.cpp:4: error: default argument given for parameter 1 of `Container::Container(int)'
container.h:12: error: after previous specification in `Container::Container(int)
what have I done wrong here?
Functions with no arguments still need the parentheses:
Container::Container() {
int y;
}
Based on your header, it looks like you just forgot the maxCapacity argument, and it should actually be:
Container::Container(int maxCapacity) {
int y;
}
(If you're asking about the warning too, it's pretty self-evident -- you declared an int x but didn't do anything with it)
EDIT: Well now you've edited it to completely change the error. Now it's an error because you're specifying the default argument in both places; you're only supposed to specify it in the declaration. Leave it out in the actual implementation, like my second example above
Your Container constructor (in the source file) should be like this:
Container::Container(int maxCapacity){
// code
}
Container::Container{
int y;
}
I'm not sure what this is intended to be. If you're trying to define your ctor, it should look something like:
Container::Container(int maxCapacity) // ....
Note that you want to include the default value in the declaration, but not in the definition.
Container::Container{
int y;
} is syntactically incorrect.
EDIT:
Try this:
Container::Container(int maxCapacity) // default argument not to be mentioned in the definition
{
int y;
}