I have this definition of the function in my class.
The .hpp file:
class SomeClass
{
public:
static string DoStuff(string s);
};
The .cpp file:
#include "header.hpp"
string SomeClass::DoStuff(string s)
{
// do something
}
Compiler says:
**error C2039: 'DoStuff' : is not a member of 'SomeClass'**
Can somebody help?
EDIT:
actual offending code
header definition
class DDateTime{
public:
static string date2OracleDate(DATE Date);
}
string DDateTime::date2OracleDate(DATE Date)
{
string s;
s="TO_DATE('" + DDateTime::DateFormat("%d/%m/%Y",Date) + "','dd/MM/YYYY')";
return s;
}
Usually, .cpp files must include the matching .h or .hpp file.
Is it the case here ?
You can also have namespace issue (missing namespace in .cpp file or static method definition outside of the namespace, and so on.).
Actually, it is difficult to answer until we have the real breaking code.
Moreover, I don't know if this is sample code, but it seems you used something like using std::string or using namespace std in your header file.
This is a bad idea because it will polute every file in which your header is included. What If someone wants to use your header file but don't want to "use" std because string is the name of one of its classes ?
Have you included the header file in your cpp file?
Maybe a namespace issue? You could have a SomeNamespace::SomeClass with a static member function and a ::SomeClass in the outer namespace without the static member function.
Are you missing
#include<string>
in your header file?
Are you trying to call DoStuff from a double pointer to your instance? Example:
SomeClass **class;
class->DoStuff();
If so do this:
SomeClass **class;
(*class)->DoStuf();
Related
In a .cpp file, an anonymous namespace basically has file-wide linkage (after #includes) because the .cpp file will never be included by another file. But, the same pattern in a header file propagates that anonymous namespace to wherever it is included in. Is there a way to create a similar effect in a header file? I ask because template implementations must be in headers.
A simple example in a regular .h file would be this:
// object.h
namespace {
using verbose::namespace::type;
}
...
struct object {
type value;
}
or similarly in some template implementation file. The type type would be in-scope of wherever this file is included.
Is there a way around this?
EDIT: I think I found a verbose but workable answer.
// object.h
struct Namespace {
using verbose::namespace::type;
Namespace() = delete;
struct object {
type value;
};
};
using Namespace::object;
This should do the trick:
// object.h
{
namespace {
using verbose::namespace::type;
}
...
struct object {
type value;
}
}
Namespaces should only be valid within the code block they're defined in.
I know how to define a class method inside of the same file with which the class is in.
For example:
class Robot
{public:
int location;
void Moves(); //Class method to be defined
}
void Robot::Moves()
{//Method definition here }
I do NOT know hot to define a class outside of the file in which the class is in. I tried creating a .hpp file and defining a class method inside of it, but my compiler signified that it could not load a class definition from a file other than the one the class was created in, or it would be as though I was placing a function's definition before the include directives.
Please note: The original class file is also in a .hpp file, as I have yet to learn how to use .cpp files other than the main one.
This is done using C++/Win32.
Create a .cpp file along these guidelines
Include your class header file in your .cpp file
Include all necessary headers you would use in main.cpp
use the scope operator for your class
#include <iostream>
#include "foo.hpp"
foo::foo()
{
// code here
}
void foo::OtherFunc()
{
// other stuff here
}
Just put your definition in a .cpp file and link it with your application.
Robot.hpp:
class Robot
{public:
int location;
void Moves(); //Class method to be defined
}
Robot.cpp:
#include "Robot.hpp"
void Robot::Moves()
{//Method definition here }
consider a fun.cpp file :
class fun
{
public:
void sum();
void dispaly();
};
Class fun2
{
public:
void subtract();
};
Now consider another c++ file execute.cpp where i want to access only subtract method of fun.cpp file..
i dont want to include "fun.cpp" file into my execute.cpp as it will increase the size(in larger projects)..
so, how can i access any particular method wihtod including the file????
i dont want to include "fun.cpp" file into my execute.cpp
Nor should you, as it would break the one definition rule (assuming the implementations are also in the cpp file).
The usual way to do this is to have the class definition in a header, and include only the header file.
To answer your question, you can, but it's fugly.
C++ allows us to define a class multiple times, as long as the definition is identical. So, in execute.cpp you can simply write:
//execute.cpp
class fun2 //note lower-case 'c'
{
public:
void subtract();
};
// use fun2 here
before you use it. But, again, the usual way is to break the class definition and implementation in a .h file and a .cpp file.
Mandatory advice: read a good introductory C++ book.
You need to include the header file which defines the class fun(and has the declaration of subtract()) in the cpp file where you want to use the function subtract().
Note that the function must be defined though.
fun2.h
#ifndef FUN2_H
#define FUN2_H
Class fun2
{
public:
void subtract();
};
#endif // FUN2_H
fun2.cpp
#include "fun2.h"
void func2::subtract()
{
}
execute.cpp
#include "fun2.h"
//use subtract() through object of `fun2`
Note that, to use a member function in a particular source file, the definition of the class which declares that function should be visible to the compiler, the actual job of linking to the particular definition is done at the linking stage and as long as you follow the above format the linker shall link the appropriate function for you.
Even though you include the file, the linker will only include code that is actually used. This is why using static libraries is sometimes preferable to a dynamic library that has to include everything.
You can't. You need to actually include the file that links to the code that defines the function if you want to include the function. If you just need the interface and aren't going to use the function, you could simply declare the class in execute.cpp as follows
class fun
{
public:
void subtract();
};
But you couldn't use subtract there.
This question already has answers here:
Is it possible to avoid repeating the class name in the implementation file?
(8 answers)
Closed 6 years ago.
If in C++ I have a class longUnderstandableName. For that class I have a header file containing its method declaration. In the source file for the class, I have to write longUnderstandableName::MethodA, longUnderstandableName::MethodB and so on, everywhere.
Can I somehow make use of namespaces or something else so I can just write MethodA and MethodB, in the class source file, and only there?
typedef longUnderstandableName sn;
Then you can define the methods as
void sn::MethodA() {}
void sn::MethodB() {}
and use them as
sn::MethodA();
sn::MethodB();
This only works if longUnderstandableName is the name of a class. It works even if the class is deeply embedded in some other namespace.
If longUnderstandableName is the name of a namespace, then in the namespace (or source file) where you want to use the methods, you can write
using namespace longUnderstandableName;
and then call methods like
MethodA();
MethodB();
You should be careful not to use a using namespace foo; in header files, because then it pollutes every .cpp file that we #include the header file into, however using a using namespace foo; at the top of a .cpp file is definitely allowed and encouraged.
Inside the methods of the classes, you can use the name without qualification, anyway: just drop the longUnderstandableName:: prefix.
In functions inside the class source file that are not methods, I suggest to introduce file-scope static inline functions, like so:
inline type MethodA(type param){
return longUnderstandableName::MethodA(param);
}
Then you can call MethodA unqualified; due to the inline nature, this likely won't cost any runtime overhead.
I'm not sure I'd recommend it, but you could use a macro like:
#define sn LongUnderstandableName
void sn::MethodA(parameters) { ... }
int sn::MethodB(parameters) { ... }
and so on. One of the bad points of macros is that they don't respect scope, but in this case, the scope you (apparently) want is the source file, which happens to correspond (pretty closely) with the scope of a macro.
Well, yes, once you understand namespaces.
Instead of naming your class MyBonnieLiesOverTheOcean, instead set up the following:
namespace My { namespace Bonnie { namespace LiesOverThe {
class Ocean { ... };
} } }
Now, when defining your methods, you put the same namespaces around the whole file, and you write:
Ocean::SomeMethod() ...
When using the class from outside all the namespaces, it's:
My::Bonnie::LiesOverThe::Ocean
If you need to reference a lot of things from some other namespace in some source file, you can use the 'use' directive to ditch the prefixes.
I have this header file, zeeheader.h, and I wrote some classes in it, I'm having problems giving a string as a parameter to one of the functions:
class DeliTest
{
public:
void DeliCheck(Stack*,string);
void ComCheck (unsigned,string);
bool EofCheck (unsigned,string);
};
As I was implementing it in the cpp file, I added #include to it, it seemed to be working, for example: as I was writing the "data." I got the "length()" appear by the intellisense, so I thought that it was working, but it wasn't. I got errors like:
syntax error : identifier 'string'
overloaded member function not found in 'DeliTest'
this is one of the functions in the cpp file:
bool DeliTest::EofCheck(unsigned i, string data)
{
if (i == data.length()-1)
return 1;
return 0;
}
Am I supposed to be adding something to the header file?
In the header file you need:
#include <string>
strings live in the std:; namespace, so your functions should look like:
void DeliCheck(Stack*, std::string);
and although it is not wrong to pass strings by value, as you are doing, it is more common and better practice to pass them by const reference:
void DeliCheck(Stack*, const std::string & );
Make sure to #include <string>, and because strings are in the std namespace, you should declare your strings as std::string in the header.
As a sidenote, make sure you do NOT declare using namespace std; in your header files. This would cause any other headers or c/cpp files that include this header to also use the std namespace. This is called polluting the namespace.
I assume you are trying to use the standard string class? If so you need to add:
#include <string>
at the top of your header file. And then also prefix your usage of string with std::; e.g.
void DeliCheck(Stack*, std::string);
You may also want to name your arguments for clarity, and you probably don't want to pass the strings by value:
void DeliCheck(Stack* s, const std::string& name);
You should #include <string> and keep in mind that string is part of the standard library so it is in the std namespace. You can either add: using namespace std; at the top of your header or fully qualify the string class with std::string where you use it. You really should get in the habit of using a fully qualified class name especially in a header file.