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.
Related
I'm very new to c++ and to my understanding if i include the header file which is where the functions are defined, i should be able to call the functions from main? I have tried to add static or public before the functions but nothing seemed to change.
//heres my main
#include "pch.h"
#include "myfile1.h"
#include <iostream>
using namespace std;
int main(void){
function1();
function2();
}
//my myfile1 header
#ifndef myfile1_h
#define myfile1_h
#include <iostream>
using namespace std;
class myClass{
void function1();
void function2();
};
#endif
//my myfile1.ccp
#include "myfile1.h"
#include <iostream>
using namespace std;
class myClass{
void function1() {
}
void function2() {
}
}
Your functions are in the myClass in myfile1. You must create object from this class, and you can use functions where in the this class. My English skills are not good but you must do this:
int main(void){
myClass myClassObject;
myClassObject.function1();
myClassObject.function2();
}
If you dont want create object from this class, you can do that :
myfile1.h:
#ifndef myfile1_h
#define myfile1_h
#include <iostream>
using namespace std;
void function1();
void function2();
#endif
myfile1.cpp:
#include "myfile1.h"
void function1(){
cout<<"function1"<<endl;
}
void function2(){
cout<<"function2"<<endl;
}
and main:
#include "myfile1.cpp"
int main(){
function1();
function2();
return 0;
}
If you want this functions with class, you can do it for good case:
myfile1.h
#ifndef myfile1_h
#define myfile1_h
#include <iostream>
using namespace std;
class myClass{
public: // If you want access these functions, you must use public tag.
// If you dont use any tag, it will be private, because default tag is private.
void function1();
void function2();
};
#endif
myfile1.cpp
#include "myfile1.h"
void myClass::function1(){
cout<<"function1";
}
void myClass::function2(){
cout<<"function2";
}
main:
#include "myfile1.h"
int main(){
myClass myClassObject;
myClassObject.function1();
myClassObject.function2();
return 0;
}
What is your compiler or editor? If you compile that codes from terminal, you must compile it like this:
g++ main.cpp myfile1.cpp
My English skills are not good. But I tried to explain.
Folks,
I crated a name space in a single file, but how exactly do I separate the definition and declaration?
Can I create a .h for declaration and a .cpp for definition just like in a class set of file? If yes, then, do I need to include the namespace header file in my new program, or is the using namspace mynamespace enough?
Any decent C++ book should cover namespaces and code separation.
Try something like this:
myfile.h
#ifndef myfile_H
#define myfile_H
namespace myns
{
class myclass
{
public:
void doSomething();
};
}
#endif
myfile.cpp:
#include "myfile.h"
void myns::myclass::doSomething()
{
//...
}
And then you can use it like this:
#include "myfile.h"
myns::myclass c;
c.doSomething();
Or:
#include "myfile.h"
using namespace myns;
myclass c;
c.doSomething();
What is going on?
#include "MyClass.h"
class MyOtherClass {
public:
MyOtherClass();
~MyOtherClass();
MyClass myVar; //Unknown type Error
};
Suddenly when I include the .h and write that var Xcode gives me tons of errors... and also the unknown type error.
How can it be unknown when the .h is included right there?
Here is the NodeButton.h file which would correspond to the MyClass.h in the example
#pragma once
#include "cinder/Vector.h"
#include "cinder/gl/gl.h"
#include "cinder/gl/Texture.h"
#include "cinder/Color.h"
#include "cinder/ImageIo.h"
#include "cinder/Timeline.h"
#include "cinder/app/AppBasic.h"
#include "cinder/App/App.h"
#include "Node.h"
#include "CursorMano.h"
using namespace ci;
using namespace ci::app;
using namespace std;
using namespace is;
typedef boost::shared_ptr<class NodeButton> NodeButtonRef;
class NodeButton : public Node2D
{
public:
NodeButton (CursorMano *cursor, string imageUrl, bool fadeIn = false, float delay = 0.0f);
virtual ~NodeButton ();
//methods
void update( double elapsed );
void draw();
void setup();
//events
bool mouseMove( ci::app::MouseEvent event );
//vars
CursorMano *mCursor;
gl::Texture mImageTexture;
Anim<float> mAlpha = 1.0f;
bool mSelected = false;
private:
};
And here are the contents of CursorMano.h which would correspond to MyOtherClass.h in the example.
#pragma once
#include <list>
#include <vector>
#include "cinder/app/AppBasic.h"
#include "cinder/qtime/QuickTime.h"
#include "cinder/gl/Texture.h"
#include "cinder/Vector.h"
#include "NodeButton.h"
using namespace ci;
using namespace ci::app;
using namespace std;
class CursorMano {
public:
CursorMano (AppBasic *app);
~CursorMano ();
void mueveMano(Vec2i);
void update();
void draw();
void play(int button);
void reset(int button);
Vec2i mMousePos;
NodeButton mButtonCaller; //this gives the unknow type error
private:
AppBasic *mApp;
gl::Texture mFrameTexture;
qtime::MovieGl mMovie;
int mIdButton;
};
You have a circular dependency of your header files.
NodeButton.h defines NodeButton class which CursorMano.h needs to include so that compiler can see definition for NodeButton but NodeButton.h itself includes CursorMano.h.
You will need to use forward declarations to break this circular dependency.
In NodeButton.h you just use an pointer to CursorMano so You do not need to include the CursorMano.h just forward declare the class after the using namespace declarations.
using namespace std;
using namespace is;
class CursorMano;
It's probably a result of the circular dependency between you two header files (NodeButton includes CursorMano and CursorMano includes NodeButton). Try removing the #include "CursorMano.h" in NodeButton.h and add class CursorMano; before your NodeButton declaration.
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
}
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.