Thread: prototype functions

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    29

    prototype functions

    I'm trying to learn how to do a prototype function and this is what is in a C++ book. I typed it out in Visual Studio, but it doesn't seem to work.
    any ideas why?




    Code:
    
    
    
    
    //draws a triangle
    void drawTriangle()
    {
    		drawIntersect();
    		drawBase();
    }
    
    // File: stickFigure.cpp
    // Draws a stick figure
    
    #include <iostream>
    using namespace std;
    
    
    // Fucntions used ...
    void drawCircle();   // Draws a circle
    
    void drawTriangle();  // Draws a triangle
    
    void drawIntersect();  // Draws intersecting lines
    
    void drawBase();       // Draws a horizontal line
    
    int main()
    {
      	
    	// Draw a circle.
    	drawCircle();
    
    	// Draw a triangle
    	drawTriangle();
    
    	// Draw intersecting lines.
    	drawIntersect();
    
    
    	return 0;
    
    }
    
    // Draw a circle
    void drawCircle()
    
    {
    	cout << "   *   " << endl;
    	cout << " *   * " << endl;
    	cout << "  * *  " << endl;
    }	// end drawCircle
    
    // Draws a triangle
    void drawTriangle
    {
    	drawIntersect();
    	drawBase();
    }  // end drawTriangle
    
    // Draws intersecting lines
    void drawIntersect()
    { 
    	cout << "   /\\   "  << endl;
    	cout << "  /  \\  "  << endl;
    	cout << " /    \\ "  << endl;
    
    }    // end drawIntersect
    
    //Draws a horizontal line
    void drawbase()
    
    {
    	cout << " _ _ _ _ _ _ " << endl;
    
    }   // end drawbase

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    How does it not work?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    29
    here are the error messages:

    error C3861: 'drawIntersect': identifier not found
    error C3861: 'drawBase': identifier not found
    error C2365: 'drawIntersect' : redefinition; previous definition was 'formerly unknown identifier'
    error C2365: 'drawBase' : redefinition; previous definition was 'formerly unknown identifier'
    error C2470: 'drawTriangle' : looks like a function definition, but there is no parameter list; skipping apparent body
    error C2365: 'drawIntersect' : redefinition; previous definition was 'formerly unknown identifier'
    7 error(s), 0 warning(s)
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Right. Remove all the code that is above:
    Code:
    // File: stickFigure.cpp
    // Draws a stick figure
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Oct 2009
    Posts
    29
    cool, that made it go from 7 errors to only 1:

    error C2470: 'drawTriangle' : looks like a function definition, but there is no parameter list; skipping apparent body

    I googled error C2470 and this is what it says:


    Code:
    Visual C++ Concepts: Building a C/C++ Program
    Compiler Error C2470
    Error Message
    'function' : looks like a function definition, but there is no parameter list; skipping apparent body
    
    
    A function definition is missing its argument list. 
    
    The following sample generates C2470:
    
    Copy Code 
    // C2470.cpp
    int MyFunc {};  // C2470
    void MyFunc2() {};  //OK
    
    int main(){
       MyFunc();
       MyFunc2();
    }

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Basically, it is saying that you forgot about the parentheses, i.e., (), when defining your MyFunc function.

    By the way, are you changing your error messages? For one thing, the line number is missing, and the error message does not seem to be as informative as I would expect.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Oct 2009
    Posts
    29
    sorry, I probably didn't cut&paste enough error info.
    I did forget the parentheses, now I understand what that sample meant, but for some reason it isn't wanting to work now that I fixed that.
    error message:

    1>------ Build started: Project: prototypeStickFigurePage124, Configuration: Debug Win32 ------
    1>Compiling...
    1>prototypeStickFigurepage124.cpp
    1>Linking...
    1>prototypeStickFigurepage124.obj : error LNK2019: unresolved external symbol "void __cdecl drawBase(void)" (?drawBase@@YAXXZ) referenced in function "void __cdecl drawTriangle(void)" (?drawTriangle@@YAXXZ)

    fatal error LNK1120: 1 unresolved externals
    1>prototypeStickFigurePage124 - 2 error(s), 0 warning(s)
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Post your updated code.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Oct 2009
    Posts
    29
    Code:
    // File: stickFigure.cpp
    // Draws a stick figure
    
    #include <iostream>
    using namespace std;
    
    
    // Fucntions used ...
    void drawCircle();   // Draws a circle
    
    void drawTriangle();  // Draws a triangle
    
    void drawIntersect();  // Draws intersecting lines
    
    void drawBase();       // Draws a horizontal line
    
    int main()
    {
      	
    	// Draw a circle.
    	drawCircle();
    
    	// Draw a triangle
    	drawTriangle();
    
    	// Draw intersecting lines.
    	drawIntersect();
    
    
    	return 0;
    
    }
    
    // Draw a circle
    void drawCircle()
    
    {
    	cout << "   *   " << endl;
    	cout << " *   * " << endl;
    	cout << "  * *  " << endl;
    }	// end drawCircle
    
    // Draws a triangle
    void drawTriangle()
    {
    	drawIntersect();
    	drawBase();
    }  // end drawTriangle
    
    // Draws intersecting lines
    void drawIntersect()
    { 
    	cout << "   /\\   "  << endl;
    	cout << "  /  \\  "  << endl;
    	cout << " /    \\ "  << endl;
    
    }    // end drawIntersect
    
    //Draws a horizontal line
    void drawbase()
    
    {
    	cout << " _ _ _ _ _ _ " << endl;
    
    }   // end drawbase

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Spot the difference:
    Code:
    void drawBase();       // Draws a horizontal line
    Code:
    //Draws a horizontal line
    void drawbase()
    
    {
    	cout << " _ _ _ _ _ _ " << endl;
    
    }   // end drawbase
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    Oct 2009
    Posts
    29
    Looks like I have a lazy shift finger, I know to check for those now...
    Thanks for your help

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Heheh, your welcome
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 13
    Last Post: 08-24-2006, 12:22 AM
  2. Function prototype questions
    By Kayoss in forum C++ Programming
    Replies: 6
    Last Post: 11-30-2005, 05:27 PM
  3. Functions and Classes - What did I do wrong?
    By redmage in forum C++ Programming
    Replies: 5
    Last Post: 04-11-2005, 11:50 AM
  4. calling functions within functions
    By edd1986 in forum C Programming
    Replies: 3
    Last Post: 03-29-2005, 03:35 AM
  5. functions - please help!!!!
    By linkies in forum C Programming
    Replies: 1
    Last Post: 08-21-2002, 07:53 AM