1

i start to code on c++, i have good background from java, i have some issue with the syntax of C++. i stuck on one thing, i create a method "SortCloums" , and i missing something on it. in the for loop its give me a massage of :

expression must have pointer-to-object type

this is the short code of my, yes i still learning about pointers. need some direction of you guys.

#include <iostream>
using namespace std;

const int size = 4;
void SortCloums(int arr[size][size], int sizeOfArray);
int main(){

int arr[size][size] = { { 0, 4, 6, 0 },
                         {5, 6 , 8, 12},
                         {50, 8, 12, 24},
                         {900, 10, 30, 50} };

SortCloums(arr, size);
    return 0;
 }

   void SortCloums(int arr ,int sizeOfArray) {
    // now we check if the array is column sorted or not.
    bool flag = true;

for (int i = 0; i < sizeOfArray && flag != false; i++){
    for (int j = 0; j < sizeOfArray && flag != false; j++){
        if (arr[j][i] > arr[j+1][i]){
            flag = false;
            std::cout << "The array of column unsorted" << endl;
            std::cin.get();
        }
    }
}
if (flag == true){
    std::cout << "The array is column sorted" << endl;
    std::cin.get();
}
    }
jacob
  • 19
  • 3
  • 12
    Take everything you learned in Java and forget it, if you want to learn the proper C++ that is. Please have a look at this [C++ books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) list. – Ron Mar 01 '18 at 21:10
  • 1
    Please read this: [mcve]. You can [edit] your question to improve its quality and your chances for getting a good answer. – anatolyg Mar 01 '18 at 21:10
  • 1
    Change `int arr[] []` to `std::vector< std::vector< int > >` – borisbn Mar 01 '18 at 21:13
  • 2
    Beware that arrays in c++ are not typical objects as in Java. Notably, they have no member methods and don't have value semantics. They are very low level mechanisms with several gotchas. Instead, use std::array for arrays with compile-time constant sizes and std::vector in other cases. This is just one example of how prior familiarity with Java (or other langues) can induce incorrect assumptions about c++. – François Andrieux Mar 01 '18 at 21:29
  • 1
    What is the problem? – DBedrenko Mar 01 '18 at 21:32
  • @borisbn in which line i need to change it and when not? – jacob Mar 01 '18 at 21:35
  • You really need to learn `C++` without reference to `Java`. They are very different languages and require very different approaches. `Java` techniques simply do not work well in `C++` (there are usually much better alternatives). – Galik Mar 01 '18 at 22:01
  • Welcome to C++ .. I think its better to read hundreds of examples before writing your test code .. – Mohammad Kanan Mar 01 '18 at 22:22
  • Also, when writing C++ you may want to check out `std::sort` and `std::is_sorted` before reinventing them yourself. – Bo Persson Mar 01 '18 at 22:27
  • i edit the post***. OK guys thank for the help. if someone can fix my code i will be happy. i just want to understand a little bit what i missing hear, and yes a goes to learn about pointers. – jacob Mar 02 '18 at 03:05
  • BTW when quoting an error message, you should point out which line of code the error message refers to. This makes your post clearer and prevents unneeded guesswork. – anatolyg Mar 05 '18 at 19:44

1 Answers1

0

I guess the error message points to this line:

    if (arr[j][i] > arr[j+1][i]){

This code treats arr as a 2-D array. However arr is declared here in an incorrect way:

void SortCloums(int arr ,int sizeOfArray) {

Because the declaration comes before usage, the compiler thinks that the declaration is correct, and usage is incorrect. So it outputs an error message that is not very clear.

To fix, declare arr correctly:

void SortCloums(int arr[size][size], int sizeOfArray) {

Here arr is conceptually a 2-D array, but for various technical reasons it's formally a pointer to an array. Your error message mentions a "pointer-to-object" type; a "pointer to array" is a special case of this, so this fixes the error.

anatolyg
  • 26,506
  • 9
  • 60
  • 134