Thread: Problem with function identifiers-

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    8

    Problem with function identifiers-

    Hi all. I'm a mechanical engineer and new to this forum. I'm doing some refresher courses on C and am trying to figure out why my program is erroring out on me. I'd appreciate any help. The program is a basic bubble sort, but when I try to compile I get

    error C3861: 'bubblesort': identifier not found
    Thanks a bunch. I'm working in Visual Studio C++ Express

    Code:
    // mod9_7.cpp : Defines the entry point for the console application.
    //
    
    #include "stdafx.h"
    #include <stdio.h>
    #include <iostream>
    #define SIZE 10
    using namespace std;
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	int i, arraybub[SIZE];
    
    	printf("Enter 10 integers to sort: \n");
    
    	for(i = 0; i < SIZE; i++)
    	{
    		printf("Integer %d: \n", i+1);
    		scanf("%d", &arraybub[i]);
    	}
    
    	printf("\nArray before sort: \n");
    	
    	for(i = 0; i < SIZE; i++)
    		printf("%d, ", arraybub[i]);
    
    	bubblesort(arraybub, SIZE);
    
    	printf("\nArray after sort: \n");
    	
    	for(i = 0; i < SIZE; i++)
    		printf("%d, ", arraybub[i]);
    
    	return 0;
    }
    
    
    void bubblesort(int array[], int nitems)
    {
      int i, j, temp;
    
      for (i = 0; i < nitems - 1; i++)
        for (j = i + 1; j < nitems; j++)
          if (array[i] > array[j]) {
            temp = array[i];
            array[i] = array[j];
            array[j] = temp;
          }
    }

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    C and C++ read files from the top down. Before you can use a function the compiler must know about it. Therefore you must prototype the bubblesort function by adding the function signature to the file before it's used. The function signature is:
    Code:
    void bubblesort(int array[], int nitems);

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    8
    Quote Originally Posted by rags_to_riches View Post
    C and C++ read files from the top down. Before you can use a function the compiler must know about it. Therefore you must prototype the bubblesort function by adding the function signature to the file before it's used. The function signature is:
    Code:
    void bubblesort(int array[], int nitems);
    Holy crap, how did I miss that. That's what you get when you program while trying to comfort a sick 1 year-old in the middle of the night. Thanks Rags.

  4. #4
    Registered User
    Join Date
    Oct 2010
    Posts
    107
    Just offering an alternative solution. You can avoid the forward declaration if you move the implementation of bubblesort before main. Not always desirable, but I thought I'd point out all the ways to deal with the problem.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    >>#include <iostream>
    Wait, what? I think you need to start making up your mind if you want to use C or C++.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Tough problem with function pointers
    By Montejo in forum C Programming
    Replies: 10
    Last Post: 12-22-2009, 01:17 AM
  2. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  4. Problem with function pointers
    By vNvNation in forum C++ Programming
    Replies: 4
    Last Post: 06-13-2004, 06:49 AM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM