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().
Related
The code given below is related to a template concept in c++.
I am not getting a proper result while passing the variables.
My expected output is swapped numbers.
However, the compiler is showing an error.
#include<iostream>
using namespace std;
template <class T>
void swap(T& a,T& b)
{
T temp;
temp=a;
a=b;b=temp;
}
int main()
{
int a1,b1;
cin>>a1>>b1;
swap(a1,b1);
cout<<a1<<endl<<b1<<endl;
}
get rid of 'using namespace std;' because in std namespace swap function template is already defined.
another solution you can specialize swap. But you can only specialize standard functions for user defined types
#include<iostream>
using namespace std;
struct Int
{
int i;
};
namespace std{
template <>
void swap<Int>(Int& a,Int& b)
{
Int temp;
temp=a;
a=b;b=temp;
}
}
int main()
{
Int a1,b1;
std::cin>>a1.i>>b1.i;
swap(a1,b1);
std::cout<<a1.i<<std::endl<<b1.i<<std::endl;
}
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 have listed my code below. I get soooo many errors saying cout and endl was not declared in this scope. I do not know what I am doing wrong or how to force the class to recognise cout? I hope I am explaining my problem correctly. If I comment out the methods (not the constructor) it works. I am probably just making a novice mistake here - please help.
using namespace std;
class SignatureDemo{
public:
SignatureDemo (int val):m_Val(val){}
void demo(int n){
cout<<++m_Val<<"\tdemo(int)"<<endl;
}
void demo(int n)const{
cout<<m_Val<<"\tdemo(int) const"<<endl;
}
void demo(short s){
cout<<++m_Val<<"\tdemo(short)"<<endl;
}
void demo(float f){
cout<<++m_Val<<"\tdemo(float)"<<endl;
}
void demo(float f) const{
cout<<m_Val<<"\tdemo(float) const"<<endl;
}
void demo(double d){
cout<<++m_Val<<"\tdemo(double)"<<endl;
}
private:
int m_Val;
};
int main()
{
SignatureDemo sd(5);
return 0;
}
The compiler needs to know where to find std::cout first. You just need to include the correct header file:
#include <iostream>
I'd suggest you not to pollute the namespace using using directives. Instead either learn to prefix std classes/objects with std:: or use specific using directives:
using std::cout;
using std::endl;
Hi I'm having a problem with global pointer being underclared in function.
Here is my code
#include <iostream>
using namespace std;
void push_l(int n);
struct elem{
int key;
elem *next;
} *left=NULL,*right=NULL;
void push_l(int n){
elem *p=left;
left=new elem;
left->key=n;
left->next=p;
if (right==NULL)right=left;
}
int main(){
push_l(5);
system "pause";
return 0;
}
This is one of the error messages I get - In function void push_l(int) left underclared
This is what you get for using namespace std; (std has a left too). And you don't even need iostream. The reference to left is ambiguous.
Do this:
#include <cstdlib>
struct elem{
int key;
elem *next;
} *left=NULL,*right=NULL;
void push_l(int n){
elem *p=left;
left=new elem;
left->key=n;
left->next=p;
if (right==NULL)right=left;
}
int main(){
push_l(5);
std::system("pause");
return 0;
}
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.