• Register
1 vote
1.4k views

Problem:

Hello good people,

I am quite familiar with java programming and willing to figure out a little bit the c++ alongside. So I’ve read the c++ documentation roughly and started coding. You’ll find my sample code below that an error, “c++ incomplete type is not allowed” 

What to do now?

#include<iostream>int main() {
    //declare the array
    int myArray[10];    //initialize and print the array
    for(int i=0;i < 10; i++) {
        myArray[i] = i;
        std::cout << myArray[i] << "\n";
}

 

12 7 7
15,250 points

Please log in or register to answer this question.

2 Answers

0 votes

Solution:

First of all, you need to read the documentation thoroughly instead of roughly before started coding in any programming language. I can see in your code you declared an array myArray as we do in java. There is a little bit difference between java a c++  declaring an array.

When we declare an array in java we do not allocate any memory in it. What it is doing is creating a pointer to the memory taken when the array is then initialized.

On the other hand, when an array is declared in C++, the memory is allocated at the time of declaration, so declaring an empty array is a big no-no because the compiler doesn’t know how much memory to reserve for the array.

This is the key difference between java and c++ in this case.
Keep discovering. Thanks

13 9 6
94,260 points
0 votes
Hi there let me help you to solve this by illustrating an example.

Classes Bat;

Class ball
{

Public:

Void play(Bat&ball);

Private:

};

Class Bat
{

Ball &ballA;

Float ball posx=800;

Private:

};

Void ball::play(Bat & ball)

{

ball.ball posx +=10;

}
Int main()

{

}

Here defining  function play requires the complete definition of class Ball.because it can access it's data member in your code.for eg: In module ball.cpp has no access to the definition of class ball which arises an error by the compiler
6 4
2,740 points

Related questions

0 votes
1 answer 18 views
18 views
Problem: I'm trying to create an entity system. Each entity has a list of components and each component has a pointer to the parent Entity. I'm getting a pointer to incomplete class type is not allowed. I know, it is because AnimalCare class in Dog header is just declared but not defined. ->Is there a way to get around this?<-
asked Apr 21 ummesalma 29.2k points
0 votes
1 answer 13 views
13 views
Problem How to pass a pointer in a function to c++
asked Mar 30 Subhendu 4.5k points
0 votes
1 answer 19 views
19 views
Problem: Each element of the array is a Number object. Is it with triple pointer? (***)
asked Jan 25 sasha 16.2k points
0 votes
1 answer 13 views
13 views
Problem: I have been making a code that allows me to use an integer pointer, which stores all the integers that I want until I type the word "end" and then print them, I did it in the following way: #include <iostream> #include <string> using namespace std; int main () ... why does that happen, and how could I correct it so that it allows me to implement the 2 codes at once? Thanks for your help.
asked Dec 25, 2020 sasha 16.2k points
0 votes
1 answer 20 views
20 views
Problem: C++ allows you to have pointers to objects. The pointers pointing to objects are referred to as Object Pointers. C++ Declaration and Use of Object Pointers Just like other pointers, the object pointers are declared by placing in front of a object pointer's name. It takes the following general form : class-name &lowast; object-pointer ;
asked Dec 25, 2020 Mashhoodch 13k points
0 votes
1 answer 18 views
18 views
I know it sounds weird but I wanted to know if there is a possibility in C ++ to write the addresses that a dynamic array saves to a file so that you can read the file and use the saved addresses with other pointers. What would be the way to enter and remove the addresses of the file?
asked Aug 14, 2020 game 4.6k points
0 votes
1 answer 6 views
6 views
Problem: Had some issues in my code recently surrounding what I now know of as a Circular dependency. In short there are two classes, Player and Ball, which both need to use information from the other. Both at some point in the code will be passed a reference of the other (from another class that will include both .h files). Please help?
asked Apr 12 Sana8989 8.9k points
0 votes
1 answer 24 views
24 views
My problem to solve is this: Make a structure called student, in which you will have the fields: name, age, average and data will be requested from the user of how many students will enter and check which of them has the best average and print it, use pointers. I would ... struct Student {      char name [30];      int age;      average float; } student [nStudents], * studentPointer = student;
asked Aug 14, 2020 game 4.6k points
0 votes
2 answers 16 views
16 views asked Jan 24 waji 1.9k points
0 votes
1 answer 14 views
14 views
Problem Hey, I Am trying to learn how to build a dynamic array of pointer in C++. i know the new operator returns the pointer of the allocated memory int*[ 10 ] of the pointer of array of int. But why we are assigning int**. I am willing to know why we are using that.    int **arr = new int*[10]; 
asked Dec 21, 2020 chris jordan 2.4k points