Hey Guys,
I had a problem where I needed to use a function with an array as one of the parameters. All I had done was the main() function and a function prototype, but it still gave an error when I tried to compile. Can somebody please help?
#include <iostream.h>
void AddBook(int BookNum, int n, int Book, int Sum);
int main()
{
int n, c, i, Sum;
cin >> n >> c;
int Book[n];
for(i = 0; i < n; i++)
{
cin >> Book[i];
}
Sum = 0;
for(i = n-1; i >= 0; i--)
{
AddBook(i, n, Book[n], Sum);
}
cin.get();
cin.ignore();
return 0;
}
1) You're not passing an array as parameter, you're passing a single int. Specifically, you're passing Book[n], which is the n+1'th element of Book, which doesn't exist. To pass your array as a parameter, you should pass it by pointer.
1 2
void AddBook(int BookNum, int n, int *Book, int Sum);
AddBook(i, n, Book, Sum);
2) The compiler error is caused by line 9. C++ allows two types of declarations, static and dynamic. In terms of arrays, it boils down to this:
- Static allocation is "easier" (more intuitive), but requires the size (here: 'n') to be known at compile time. This means it must be a constant or a literal:
1 2 3
constint n = 6;
int Book[n];
int Book2[10];
-Dynamic allocation is "confusing", but allows you to set the size during runtime:
1 2 3 4 5
int n;
cin >> n;
int *Book = newint[n];
// Code that uses Book, until no longer required.
delete [] Book;
#include <iostream.h>
#include <vector>
usingnamespace std;
void AddBook(int BookNum, int n, int Book, int Sum);
int main()
{
vector<int> Book; // declare the vector
int n, c, i, Sum, input;
cin >> n >> c;
for(i = 0; i < n; i++)
{
cin >> input;
Book.push_back(input); // insert values into the vector.
}
Sum = 0;
for(i = n-1; i >= 0; i--)
{
AddBook(i, n, Book[n], Sum);
}
cin.get();
cin.ignore();
return 0;
}
#include <iostream.h>
void AddBook(int BookNum, int n, int Book, int Sum);
int main()
{
int n, c, i, Sum;
cin >> n >> c;
int *Book = newint[n]; // dynamically allocate memory to a pointer
for(i = 0; i < n; i++)
{
cin >> Book[i];
}
Sum = 0;
for(i = n-1; i >= 0; i--)
{
AddBook(i, n, Book[n], Sum);
}
delete [] Book; // delete the allocated memory after using
cin.get();
cin.ignore();
return 0;
}
vectors should be simpler and preferable though I believe.
none of the solutions seem to be working ....
@Dacster & Maniax & Gaminic - Your solutions are giving the error:
Error 1 error LNK2019: unresolved external symbol "void __cdecl AddBook(int,int,int *,int)" (?AddBook@@YAXHHPAHH@Z) referenced in function _main c:\Users\Tejas\documents\visual studio 2010\Projects\Siruseri Book Store\Siruseri Book Store\main.obj
Can someone suggest how I put a dynamic array as a parameter in a function ??
Well, you haven't defined your function anywhere. It doesn't know what to do. I thought you left it out because it didn't matter, but apparently it's just missing.
A function needs two things: a declaration ('it exists') and a definition ('this is what it does'):
1 2 3 4 5 6 7
// Declare function
void myFunction(T myParam);
// Definition
void myFunction(T myParam) { cout << myParam+2; }
// Calling
T randomParam = 6; // Assume it's an int-ish type.
myFunction(randomParam);
Often, the declaration and definition are done simultaneously (in which case the declaration is simply skipped), but forward-declaration is often useful (or required), for instance when to functions need each other.
If you have an explicit declaration, you need to make sure it's the exact same (name, return type and parameter list) as the definition.
undefined reference means that the compilation worked fine, but now the linker is looking for the compiled code for that function, to make the program, and it can't find it.
Really? Oh ok thanks then. I just wanted to check if the code was error-free before I started putting the code for my function. Thanks a lot Moschops (and all the others who helped) !!!