build own library (static)

Dear All,

I am just learning how to build the static library on my own using visual C++ 2008.

I used the following code to create the header file and cpp of the library.but I am unable to compile due to the following error. So can anyone please help me out to understand this issue.

// MathFuncsLib.h

namespace MathFuncs
{
class MyMathFuncs
{
public:
// Returns a + b
static double Add(double a, double b);

// Returns a - b
static double Subtract(double a, double b);

// Returns a * b
static double Multiply(double a, double b);

// Returns a / b
static double Divide(double a, double b);
};
}


// MathFuncsLib.cpp
// compile with: cl /c /EHsc MathFuncsLib.cpp
// post-build command: lib MathFuncsLib.obj

#include "MathFuncsLib.h"

#include <stdexcept>

using namespace std;

namespace MathFuncs
{
double MyMathFuncs::Add(double a, double b)
{
return a + b;
}

double MyMathFuncs::Subtract(double a, double b)
{
return a - b;
}

double MyMathFuncs::Multiply(double a, double b)
{
return a * b;
}

double MyMathFuncs::Divide(double a, double b)
{
return a / b;
}
}



error I get while it is compiled :
Error 3 error C2653: 'MyMathFuncs' : is not a class or namespace name

Can you print the full error message, you appear to have trucated it. In fact, in this case, can you post the full output from the output window.

Can you use the code format tag to format your source code.
Thanks Kbw. Please have a check


//cpp file
#include "MathFuncsLib.h"
#include "stdafx.h"
#include <stdexcept>

using namespace std;

namespace MathFuncs
{
double MyMathFuncs::Add(double a, double b)
{
return a + b;
}

double MyMathFuncs::Subtract(double a, double b)
{
return a - b;
}

double MyMathFuncs::Multiply(double a, double b)
{
return a * b;
}

double MyMathFuncs::Divide(double a, double b)
{
return a / b;
}
}

//header file

#include "stdafx.h"
namespace MathFuncs
{
class MyMathFuncs
{
public:
// Returns a + b
static double Add(double a, double b);

// Returns a - b
static double Subtract(double a, double b);

// Returns a * b
static double Multiply(double a, double b);

// Returns a / b
static double Divide(double a, double b);
};
}


Error:

Error 2 error C2653 (line 9) : 'MyMathFuncs' : is not a class or namespace name
There's nothing wrong with what you have posted. If you have a problem, it must be with something else that you haven't disclosed.
remove everything that has to deal with namespace and leave everything else.

like just delete namespace { } leave the stuff you got inside the namespace.

leave using namespace std though.
Last edited on
Topic archived. No new replies allowed.