Why wont this compile?

Pages: 12
Template:

#ifndef MATHLIBRARY_H_INCLUDED
#define MATHLIBRARY_H_INCLUDED

#include <iostream>
#include <cmath>

using namespace std;

namespace MyNamespace
{

template<typename T>
T Area(T height, T length)
{
return height * length;
}

const double PI = 4.0*atan(1.0);

template<typename T>
T CircleArea(T radius)
{
double result;

result = PI * radius * radius;

return (T)result;

}

template<typename T>
T TriangleArea(T base, T height)
{
double result;

result = base * height * 0.5;

return (T)result;
}

}

#endif // MATHLIBRARY_H_INCLUDED




Test Code:

#include <iostream>
#include "..\MathLibrary\MathLibrary.h"

using namespace std;
using namespace MyNamespace;

int main()
{
cout << "4 X 4 Areas:" << endl;
cout << "Square: " << Area<int>(4, 4) << endl;
cout << "Circle: " << CircleArea<int>(2) << endl;
cout << Triangle: " << TriangleArea<int>(4, 4) << endl;
cout << Using a value of pi of: " << PI << endl;
return 0;

}

cout << Triangle: " << TriangleArea<int>(4, 4) << endl;
Only one "

cout << Using a value of pi of: " << PI << endl;
Only one "


Both of these errors were marked out very clearly by your compiler's error messages.
closed account (1vRz3TCk)
1
2
cout << Triangle: " << TriangleArea<int>(4, 4) << endl;
cout << Using a value of pi of: " << PI << endl;
missing some "?

1
2
cout << "Triangle: " << TriangleArea<int>(4, 4) << endl;
cout << "Using a value of pi of: " << PI << endl;
Thanks guys I also have another question do you know how to fix the host application to run a library problem that I am coming across? If you do that would be great and I'm talking like any methods that I should do the fix it or something along those lines.
1
2
3
4
5
6
int main()
{

  // put code here to use library objects and functions
  return 0;
}
Do you mean the test code? Please if you could give an example and will it fix my problems?
Last edited on
closed account (1vRz3TCk)
Please give more information about what IDE you are using and how you have setup your solution/projects.

If you have two separate projects, one for the library and one for the test code, look at which one is set to be the 'startup project' in the IDE. If it is the library change it to the test one.
That is an example. If you want to use functions and/or objects that are provided in a library, you just call them from your programme, #include any headers that come with the library, and link to the library at link time.
How do I check which one is the startup project in the IDE?
How do I check which one is the startup project in the IDE?


Depends which IDE you're using. Many IDEs don't have anything called a "startup project". What does that have to do with using a library?
Last edited on
So how do I fix this problem that I'm having because for some reason it's not allowing me to compile.
We already told you. Your code above is missing some " Codemonkey even wrote out for you the correct code.
Sorry I didn't mean it like that, I meant why when I compile it says "you must select a host application to run a libary...?"
You've got your IDE settings all screwed up. It's got absolutely nothing to do with C++. I suggest you ask in a forum dedicated to your chosen IDE.
Last edited on
How so? what should I do to fix them?
It's got absolutely nothing to do with C++. I suggest you ask in a forum dedicated to your chosen IDE.
Why are you not giving the name of the ide .. which you are using .
As an aside, this is one of the reasons why I advocate not giving new programmers IDEs. We end up thrashing around making the IDE work instead of teaching them how to programme. Obviously other opinions differ. :)
Last edited on
Codeblocks
http://forums.codeblocks.org/

Somewhere in your project options, you've told Code::Blocks that you're making a library.
Pages: 12