Thread: compiling multiple source files

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    15

    compiling multiple source files

    Hey all, for my next assignment, I decided to try and compile from multiple source files. One as a header for function and structure definitions, a cpp file for declarations, and a main for a driver. The code looks something like this:

    Header:
    Code:
    #include <iostream>
    using namespace std;
    
    struct point
    {   double x;
        double y;
    };
    
    struct circle
    {   point center;
        double radius;
    };
    
    struct triangle
    {
        point a;
        point b;
        point c;
    };
    
    double Distance (point a, point b);
    
    double areaOfcircle (double radius);
    
    double Perimeter (double radius);
    
    void triangleDistance( point a, point b, point c);
    Main program file:
    Code:
    #include <iostream>
    #include "untitled.h"
    using namespace std;
    int main()
    {
        point p1;
        point p2;
        p1.x =  1;
        p1.y =  2;
        p1.x = -1;
        p1.x = -2;
        cout << Distance (p1,p2) << endl;
    	
        circle a;
        a.center.x = 1;
        a.center.y = 2;
        a.radius = 3;
        cout << areaOfcircle (a.radius) << endl;
        cout << Perimeter (a.radius) << endl;
    	
        point b;
        point c;
        point d;
        b.x = 1;
        b.y = 2;
        c.x = 3;
        c.y = 4;
        d.x = 5;
        d.y = 6;
    	triangleDistance(b,c,d);
        return 0;
    
    }
    And Function Declaration file (this is the file that's giving me errors):
    Code:
    /*
    #include "untitled.h"
    //Function Declarations
    
    #include <iostream>
    #include <cmath>
    using namespace std;
    const double pi = 3.14159265358979323846;
    
    double Distance (point a , point b )
    {
        double answer = sqrt ((a.x-b.x)(a.x-b.x) + (a.y-b.y)(a.y-b.y) );
        return answer; // The error states : '(a.point::x-b.point::x)' cannot be used as a function
    }
    double areaOfcircle (double radius)
    {
        double answer = pi * radius* radius;
        return answer;
    }
    double Perimeter (double radius)
    {
        double answer = 2 * pi * radius;
        return answer;
    }
    void triangleDistance( point b, point c, point d)
    {
    	double answer = sqrt ((b.x-c.x)(b.x-c.x) + (b.y-c.y)(b.y-c.y) ); // The error states : '(a.point::x-b.point::x)' cannot be used as a function
    	cout << "length of side 'a' is: "<< answer;
    	double answer2 = sqrt ((b.x-d.x)(b.x-d.x) + (b.y-d.y)(b.y-d.y) ); // The error states : '(a.point::x-b.point::x)' cannot be used as a function
    	cout << "length of side 'a' is: "<< answer2;
    	double answer3 = sqrt ((c.x-d.x)(c.x-d.x) + (c.y-d.y)(c.y-d.y) ); // The error states : '(a.point::x-b.point::x)' cannot be used as a function
    	cout << "length of side 'a' is: "<< answer3;
    	return;
    	}
    I've never split up a project into different files before, and I'm only just learning about structures, so I'm not sure what my error in the declaration file is, much less understand what the compiler's trying to say. Thanks in advance for any and all help.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    >>(a.x-b.x)(a.x-b.x) + (a.y-b.y)(a.y-b.y)
    You cannot perform multiplication this way. Write out the multiplication operator *.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Feb 2011
    Posts
    15
    Oh. Two hours well spent. Thank you very much for the help. Out of curiousity, could you explain what using the wrong operator has anything to do with the error message I received?

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The compiler is confused. I believe it interprets (a.x-b.x) as a cast, but then sees (a.x-b.x) afterwards, thinking it is some function pointer cast or the like and tries to interpret it as a function call. But that doesn't work, obviously.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    C++ Junkie Mozza314's Avatar
    Join Date
    Jan 2011
    Location
    Australia
    Posts
    174
    '(a.point::x-b.point::x)' cannot be used as a function
    The compiler is saying that the subtraction of these values is not a function. Because (a.x - b.x) is a value, the parentheses following it are interpreted as the function call operator; hence the compiler complains you are trying to use (a.x - b.x) as a function.

    It's possible for the expression

    Code:
    (a.x - b.x)(a.x - b.x)
    to not produce an error. Observe:

    Code:
    #include <iostream>
    
    struct Strange
    {
        Strange& operator-(Strange& rhs) { return *this; }
        void operator()(Strange&) { std::cout << "Strange's operator() called" << std::endl; }
    };
    
    struct Point
    {
        Strange x;
        Strange y;
    };
    
    int main()
    {
        Point a, b;
        (a.x - b.x)(a.x - b.x);
    
        return 0;
    }
    Output:
    Code:
    Strange's operator() called

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to create a C project with multiple source files
    By MSF1981 in forum C Programming
    Replies: 5
    Last Post: 03-22-2009, 09:25 AM
  2. Compiling Multiple files in a single Project
    By pdwivedi in forum C Programming
    Replies: 2
    Last Post: 10-08-2007, 05:14 AM
  3. compiling source files and header file at once?
    By 1jackjack in forum C Programming
    Replies: 10
    Last Post: 05-04-2007, 11:06 AM
  4. Need help with input streams from multiple source files
    By orikon in forum C++ Programming
    Replies: 2
    Last Post: 10-08-2005, 02:56 PM
  5. Multiple Source Files!?!?
    By Padawan in forum C Programming
    Replies: 14
    Last Post: 04-04-2004, 12:19 AM