7 Replies - 3325 Views - Last Post: 07 December 2012 - 02:58 PM Rate Topic: -----

#1 indigojoker   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 23-October 12

Class Functions- Expected Primary-Expression Error in Main

Posted 06 December 2012 - 06:53 PM

I seem to always have trouble with this sort of thing. I have tried k.getname(char*), k.getname(char), k.getname(char[]) etc... I know the error says something else is expected before "char," but I can't figure out what.

#include <iostream>

using namespace std;

class Person
{
    private:
    char name[];

    public:
    Person();
    Person(char[]);
    void getname(char[]);

};
void Person::getname(char[])
{
    char name[20];
    cout << "Please enter your name: ";
    cin >> name;
}
int main()
{
   Person k;
   k.getname(char[]);   <-error: expected primary-expression before 'char'
}


Is This A Good Question/Topic? 0
  • +

Replies To: Class Functions- Expected Primary-Expression Error in Main

#2 jimblumberg   User is offline

  • member icon

Reputation: 5519
  • View blog
  • Posts: 17,109
  • Joined: 25-December 09

Re: Class Functions- Expected Primary-Expression Error in Main

Posted 06 December 2012 - 09:08 PM

Please post the complete error message exactly as it appears in your development environment. These messages have important information embedded within them to aid in locating and fixing these errors.

You may also want to study the function tutorials provided in my signature. They should help explain how to create and use functions.

Jim

This post has been edited by jimblumberg: 06 December 2012 - 09:09 PM

Was This Post Helpful? 1
  • +
  • -

#3 indigojoker   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 23-October 12

Re: Class Functions- Expected Primary-Expression Error in Main

Posted 06 December 2012 - 10:45 PM

Thank you for your reply, I will definitely look into those tutorials.

C:\Users\Ned\Desktop\C++ Files\main.cpp|57|error: expected primary-expression before 'char'|

This is all it says. Although it says line 57, I have removed other classes to make it easier to read. But the other classes and function definitions build on the getname function, and are difficult to write when I am distracted by this simple problem.

In case it matters, here is everything I have:

#include <iostream>
#include <cstdlib>

using namespace std;

class Person
{
    private:
    char name[];

    public:
    Person();
    Person(char[]);
    void getname(char[]);

};
void Person::getname(char[])
{
    cout << "Please enter your name: ";
    cin >> name;

}


class Account : Person
{
    private:
    int actno;
    float balance;


    public:
    Account();
    Account(int, char[]);

    void setbal(float);
    void deposit(float);
    float withdraw(float);
    float getbal();
    void print_bal();
};
class Business : Account
{
    private:
    float chkfee;

    public:
    float withdraw(float);
    Business();
    Business(int, char[]);
    void setfee(float);
};

int main()
{
   Person k;
   k.getname(char);
}


Was This Post Helpful? 0
  • +
  • -

#4 AKMafia001   User is offline

  • </code.in.dream>
  • member icon

Reputation: 238
  • View blog
  • Posts: 738
  • Joined: 11-June 11

Re: Class Functions- Expected Primary-Expression Error in Main

Posted 07 December 2012 - 03:05 AM

Well! As suggested by jimblumberg you really need to read about functions..

As of your code:
You are trying to have a static array, so this should have a size:
09	    char name[];


The name array inside the constructor is different than the data member name of the class, this one is local and is destroyed as it goes out of scope...
16	void Person::getname(char[])
17	{
18	    char name[20];
19	    cout << "Please enter your name: ";
20	    cin >> name;
21	}



The function header should declare a parameter:
16	void Person::getname(char[])


You need to pass an argument to the function:
57	   k.getname(char);


Learn the basics first before jumping into classes and stuff(OOP)...
Was This Post Helpful? 1
  • +
  • -

#5 indigojoker   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 23-October 12

Re: Class Functions- Expected Primary-Expression Error in Main

Posted 07 December 2012 - 02:31 PM

I think I'm picking up what you're throwing down.(And you're right about function basics, I really should know that stuff but I've been pretty scatter brained lately)

If this is what you suggested, it still presents the same error code. (I have made some other minor changes, I'm trying everything I can think of, this is so frustrating!!!)

#include <iostream>
#include <cstdlib>

using namespace std;

class Person
{
    private:
    char name[40];

    public:
    Person();
    Person(char input[]);
    void getname(char input[]);

};

class Account : Person
{
    private:
    int actno;
    float balance;


    public:
    Account();
    Account(int, char[]);

    void setbal(float);
    void deposit(float);
    float withdraw(float);
    float getbal();
    void print_bal();
};
class Business : Account
{
    private:
    float chkfee;

    public:
    float withdraw(float);
    Business();
    Business(int, char[]);
    void setfee(float);
};

int main()
{
    Person k;

    cout << "Please enter name for the account: \n";

    k.getname(char input);

}

void Person::getname(char input[])
{
    cin >> input;
}


C:\Users\Ned\Desktop\C++ Files\main.cpp|53|error: expected primary-expression before 'char'|
Was This Post Helpful? 0
  • +
  • -

#6 indigojoker   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 23-October 12

Re: Class Functions- Expected Primary-Expression Error in Main

Posted 07 December 2012 - 02:38 PM

Also the homework calls for using char arrays instead of c-strings
Was This Post Helpful? 0
  • +
  • -

#7 jimblumberg   User is offline

  • member icon

Reputation: 5519
  • View blog
  • Posts: 17,109
  • Joined: 25-December 09

Re: Class Functions- Expected Primary-Expression Error in Main

Posted 07 December 2012 - 02:57 PM

Quote

Also the homework calls for using char arrays instead of c-strings

C-strings are character arrays.

Quote

C:\Users\Ned\Desktop\C++ Files\main.cpp|53|error: expected primary-expression before 'char'|

The problem is referring to this line:
    k.getname(char input);

And this is why I recommend you thoroughly study the function tutorials in my signature. This is not how you call a function.


Jim
Was This Post Helpful? 0
  • +
  • -

#8 AKMafia001   User is offline

  • </code.in.dream>
  • member icon

Reputation: 238
  • View blog
  • Posts: 738
  • Joined: 11-June 11

Re: Class Functions- Expected Primary-Expression Error in Main

Posted 07 December 2012 - 02:58 PM

First, char arrays are actually C-Strings... So don't worry about that... ;)

The functions accepts a char array, not just a single char. Also, don't declare the array within the function call... Do it like this:
char chArray[24];
k.getname(chArray);



Second, Person is the base class and you have derived two other classes from it. Don't you wanna use the most derived class and instantiate an object of it instead of Person's object?

I think in that case, you need some knowledge of constructor initialization lists to learn how to initialize members of the parent classes...

Also, here is an example I wrote some days go, it might help!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1