guys i am new to c++
i am trying to create an class these are my files
//main.cpp
#include <iostream>
#include "testing/test.h"
#include <string>
using namespace std;
int main(void)
{
test c;
c.set_url("e");
}
test.h
#ifndef TEST_H_
#define TEST_H_
#include<string>
class test {
public:
void testing(string url);
};
#endif /* TEST_H_ */
//test.cpp
#include <iostream>
#include<string>
using namespace std;
void crawl::testing (string url) {
cout<< "i am from test class";
}
i am getting error: ‘string’ has not been declared error
The problem is you need to use the fully qualified name of string since the std namespace is not imported
class test {
public:
void testing(std::string url);
};
Please avoid the temptation to fix this by using the std namespace within the testing.h file. This is generally speaking bad practice as it can change the way names are resolved. It's much safer, albeit a bit annoying, to qualify names in header files.
The error you are getting is from not using the namespace std in the header file ie. std::string and not having the using namespace std; before your inclusion of the header file (or in the header).
The command using namespace std; is saying assume a class may be in this namespace, but it only applies to all the uses after the command.
If you did this instead it would also work, though in general this is bad form.
#include <string>
using namespace std;
#include "testing/test.h"
Also, don't forget to include test.h into test.cpp.
Related
I am trying to compile my code, but I am getting an error with classes. One of the classes compiled fine(Example.h), but the other(Name.h) keeps giving me the class does not name type error. I think it has something to do with circular dependency, but how do I fix that without a forward deceleration?
Name.h
#ifndef _NAME
#define _NAME
#include <iostream>
#include <string>
using namespace std;
#include "Example.h"
class Name{
};
#endif
Example.h
#ifndef _EXAMPLE
#define _EXAMPLE
#include <iostream>
#include <string>
using namespace std;
#include "Name.h"
class Example{
};
#endif
I saw a post about using forward deceleration, but I need to access memebers from the Example class..
You have a circular dependency, where each header tries to include the other, which is impossible. The result is that one definition ends up before the other, and the name of the second is not available within the first.
Where possible, declare each class rather than including the entire header:
class Example; // not #include "Example.h"
You won't be able to do this if one class actually contains (or inherits from) another; but this will allow the name to be used in many declarations. Since it's impossible for both classes to contain the other, you will be able to do this (or maybe just remove the #include altogether) for at least one of them, which should break the circular dependency and fix the problem.
Also, don't use reserved names like _NAME, and don't pollute the global namespace with using namespace std;
see, here you are including #include "Example.h" in Name.h and #include "Name.h" in Example.h. suppose compiler compiles Name.h file first so _NAME is defined now, then it tries to compile Example.h here compiler wants to include Name.h but the content of Name.h will not be included in Example.h since _NAME is already defined, hence class Name is not defined inside Example.h.
you can explicitly do forward declaration of class Name; inside Example.h
Try this:
Name.h
#ifndef NAMEH
#define NAMEH
#include <iostream>
#include <string>
using namespace std;
//#include "Example.h"
class Example;
class Name{
};
#endif
Name.cpp
#include "Name.h"
#include "Example.h"
...
Example.h
#ifndef EXAMPLEH
#define EXAMPLEH
#include <iostream>
#include <string>
using namespace std;
//#include "Name.h"
class Name;
class Example{
};
#endif
Example.cpp
#include "Example.h"
#include "Name.h"
...
Im wanted to create a bidirectional association between 2 classes. For example class A has class B as its private attribute and class B has class A as its private attributes.
Errors which I have gotten is mainly:
Error 323 error C2653: 'Account' : is not a class or namespace name
Error 324 error C2143: syntax error : missing ';' before '{'
(i get loads of such error)
I believe these errors got to do with how i include paymentMode.h in account.h and vice versa. I tried commenting off one inclusion in one of the classes and things work fine. May I ask how to remove such errors while I can still have my bidirectional association between account and paymentMode class?
Thank you!
Attached are the codes that I have written.
//paymentMode.h
#pragma once
#ifndef _PAYMENTMODE_H
#define _PAYMENTMODE_H
#include <string>
#include <iostream>
#include <vector>
#include "item.h"
#include "account.h"
using namespace std;
class PaymentMode
{
private:
string paymentModeName;
double paymentModeThreshold;
double paymentModeBalance; //how much has the user spent using this paymentMode;
vector<Account*> payModeAcctList;
public:
PaymentMode(string);
void pushItem(Item*);
void addAcct(Account*);
string getPaymentModeName();
void setPaymentModeName(string);
void setPaymentModeThreshold(double);
double getPaymentModeThreshold();
void setPaymentModeBal(double);
double getPaymentModeBal();
void updatePayModeBal(double);
int findAccount(string);
void deleteAccount(string);
};
#endif
//account.h
#pragma once
#ifndef _ACCOUNT_H
#define _ACCOUNT_H
#include <string>
#include <iostream>
#include <vector>
#include "paymentMode.h"
using namespace std;
class Account
{
private:
string accountName;
//vector<PaymentMode*> acctPayModeList;
double accountThreshold;
double accountBalance; //how much has the user spent using this account.
public:
Account(string);
//void addPayMode(PaymentMode*);
//int findPayMode(PaymentMode*);
string getAccountName();
void setAccountName(string);
void setAccountThreshold(double);
double getAccountThreshold();
void setAccountBal(double);
double getAccountBal();
void updateAcctBal(double);
};
#endif
You have a circular include dependency, but in this case, since class A only holds a container of pointers of class B, and vice versa, you can use forward declarations, and put the includes in the implementation files.
So, instead of
#include "account.h"
use
class Account;
Unrelated: do not put using namespace std in header files, and if possible, nowhere. See here for more on that issue.
I have a file GetL.hxx
#ifndef GetL_included
#define GetL_included
#include <iostream>
using namespace std;
class GetL
{
public:
virtual int getWidth();
};
#endif //GetL_include
Here the class GetL contains only one virtual function. what should i put in source file i.e. in GetL.cxx
#include "GetL.hxx"
int GetL::getWidth() {
// your code goes here
}
By the way, having using namespace std; in a header file is not a good practice.
I have a my.h file:
#ifndef __MY__
#define __MY__
#include <string>
#include <time.h>
class S
{
public: S();
std::string myname;
};
#endif
my.cpp
#include "my.h";
#include<string>
#include<iostream>
using namespace std;
S::S()
{
// .. code
}
I want to create an so file. There is no error when creating it. But when I compile the .h file it says: string:No such file or directory. If I pus string.h instead of string I have the error: expected '=',',',';','asm', before S (at class S) in my.h.
In the .cpp file (if i change the string with string.h) i have after i compile error: string in namespace std does not name a type. WHERE AM I WRONG?
Well, first, it seems that you come from java because when you typed:
class S
{
public: S();
std::string myname;
};
I guess you actually meant:
class S
{
public:
S();
private:
std::string myname;
};
In the .cpp file, you typed s instead of S: note that C++ is case-sensitive regarding classes names.
Also, regarding your problem, I suspect you are currently using a C compiler and not a C++ compiler. Without knowing the used command-line, I can't say much more on that.
Try this
#ifndef MY_H
#define MY_H
#include <string>
#include <time.h>
class S
{
public: S();
std::string myname;
};
#endif
#include "my.h"
#include<string>
#include<iostream>
using namespace std;
S::S()
{
//code
}
Here's the whole code getting the errors:
Engine.h
#ifndef ENGINE_H
#define ENGINE_H
#include "DXManager.h"
namespace XEngine
{
class Engine
{
};
}
#endif
DXManager.h
#ifndef DX_MANAGER_H
#define DX_MANAGER_H
namespace XEngine
{
class Engine; // forward declaration
class DXManager
{
public:
void run(Engine *engine);
};
}
#endif
DXManager.cpp
#include <iostream>
#include "Engine.h"
#include "DXManager.h"
using namespace XEngine;
void DXManager::run(Engine *engine)
{
std::cout<<"DXManager::run"<<std::endl;
}
With these 30 lines of code, I'm getting 20 errors like:
'XEngine' : a namespace with this name does not exist
'XEngine' : a symbol with this name already exists and therefore this name cannot be used as a namespace name
syntax error : identifier 'Engine'
Clearly, I'm missing something important here. What am I doing wrong?
note: I am aware that circular dependency is a bad thing, but in my particular case I believe that it is relevant.
In DXManager.cpp you are not just using some names from namespace XEngine. You define the function in that namespace.
So must be:
DXManager.cpp
#include <iostream>
#include "Engine.h"
#include "DXManager.h"
namespace XEngine {
void DXManager::run(Engine *engine)
{
std::cout<<"DXManager::run"<<std::endl;
}
}
AFAIK some of the compilers (like MSVC) process using variant too.
But it is not correct because your syntax tries to define function ::DXManager::run - not ::XEngine::DXManager::run you intend to define.
In the forward-declaration of class Engine the namespace XEngine doesn't exist at this point.
A workaround would be moving the declaration inside the namespace block.
When Engine.h includes DXManager.h, the latter defines a class XEngine::Engine without declaring the namespace first.