Thread: expected for function style cast or type constructor

  1. #1
    Registered User
    Join Date
    Aug 2014
    Posts
    7

    expected for function style cast or type constructor

    Hi. I am tackling the following problem: write the function that builds the multiplication table of arbitrary dimensions. I thought I built the function correctly, but when I call it in main, the variables I declare x and y give me the exceptions "expected for function style cast or type constructor". Is there something I am forgetting? It's really bugging me.

    Code:
    #include <iostream>
    
    
    using namespace std;
    
    
    void create_table(int num_row, int num_col) {
        int **table; //initialize pointer to pointer of int
        table = new int*[num_row]; //create new space for array of int depending on user input
        
        for (int r=0; r<num_row; r++) {
            table[r] = new int[num_col]; //assign pointer to rows of table
        }
       
        for ( int r = 0; r < num_row; r++) {
            for ( int c = 0; c < num_col; c++) {
                **table = r*c;
                cout << **table << "/t";
                
            }
        }
        
        for (int row = 0; row < num_row; row++) {
            delete [] table[row];
            }
        delete [] table;
    }
    
    
    
    
    
    
    
    
    int main[] {
        int y;
        int x;
        cout << "Please enter number of rows: ";
        cin >> y;
        cout << "Please enter number of columns: ";
        cin >> x;
        create_table(y, x);
        return 0;
       
    };

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Code:
        for ( int r = 0; r < num_row; r++) {
            for ( int c = 0; c < num_col; c++) {
                **table = r*c;
                cout << **table << "/t";
                 
            }
        }
    No reason to set up a matrix of integers if you are just going to print it inside of that loop. You aren't using any element besides table[0][0] right now. Print (r * c). It's the exact same.

    Code:
    int main[] {
    int main()
    Function blocks do not need a trailing semi-colon.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
        int **table; //initialize pointer to pointer of int
        table = new int*[num_row]; //create new space for array of int depending on user input
         
        for (int r=0; r<num_row; r++) {
            table[r] = new int[num_col]; //assign pointer to rows of table
        }
    std::vector<std::vector<int>> table(num_row, std::vector<int>(num_col));

    Please learn to use vectors instead of manual allocations.
    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.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by nlp412 View Post
    Code:
        for ( int r = 0; r < num_row; r++) {
            for ( int c = 0; c < num_col; c++) {
                **table = r*c;
                cout << **table << "/t";
                
            }
        }
    This loop is writing a value to **table (i.e. table[0][0]) repeatedly.

    The compiler error will be because arguments of an function are supplied in normal brackets, not square brackets.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Oh yes, and you probably meant for "/t" to be "\t". Escape characters use a backslash, no a forward slash.
    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.

  6. #6
    Registered User Alpo's Avatar
    Join Date
    Apr 2014
    Posts
    877
    Quote Originally Posted by grumpy View Post
    This loop is writing a value to **table (i.e. table[0][0]) repeatedly.

    The compiler error will be because arguments of an function are supplied in normal brackets, not square brackets.
    I was looking at this thing for like 15 minutes yesterday looking for where a cast was taking place, and never realized the main function had square brackets.... @.@ -facepalm-

    You must have the eyes of an eagle or something.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 04-18-2013, 01:50 PM
  2. Replies: 3
    Last Post: 06-05-2012, 01:14 PM
  3. Expected constructor, destructor, type conversion
    By SterlingM in forum C++ Programming
    Replies: 6
    Last Post: 03-26-2010, 01:16 PM
  4. expected constructor, destructor, or type conversion before '('
    By cosmiccomputing in forum C Programming
    Replies: 5
    Last Post: 06-16-2008, 11:03 PM
  5. reinterpret_cast, C-style cast or function-style cast
    By blight2c in forum C++ Programming
    Replies: 3
    Last Post: 05-14-2002, 10:07 PM