13 Replies - 1633 Views - Last Post: 26 April 2012 - 03:37 AM Rate Topic: -----

#1 fml.max  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 10-December 09

Populating char array in for loop and class problem

Posted 24 April 2012 - 03:26 PM

Hey. I'm currently writing a program which various functions. One of the functions outputs a char which I want to read into a char array via a for loop as each iteration requires the user to input a letter. The code is shown below
                char choice_2[numberofelements];
                double pimpedance[numberofelements];
                for(int i=0; i<numberofelements; i++)
                {
                    choice_2[i]=addelementsmenu(); //Problem occurs here. User input retrieved by function 
                    pimpedance[i]=elementinput(choice_2);
                    a.addelementparalell(choice_2,pimpedance,numberofelements);
                }

            }




Thanks for your help!



Also I get what a vtable is but this line gives me an error undefined reference to vtable:

resistor(double pimpedance, char pconnect):circuitelement(pconnect){complex<double>impedance(pimpedance,0);};


I reckon it comes from the constructor.

NewSeries= new resistor(pimpedance,'s');



where NewSeries is a base class pointer

Thx for your help

Is This A Good Question/Topic? 0
  • +

Replies To: Populating char array in for loop and class problem

#2 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1622
  • View blog
  • Posts: 5,709
  • Joined: 03-August 09

Re: Populating char array in for loop and class problem

Posted 24 April 2012 - 03:31 PM

please post the error messages exactly as you see them; they contain important information that will help us to help you.
Was This Post Helpful? 0
  • +
  • -

#3 fml.max  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 10-December 09

Re: Populating char array in for loop and class problem

Posted 25 April 2012 - 10:49 AM

Sorry,

Btw, the compiler used is g++ which uses strict ISO rules to be compilable on all possibilities.

Circuits/main.cpp|191|error: invalid conversion from ‘char*’ to ‘char’ [-fpermissive]|


The other error is

Circuits/circuitelement.h|52|undefined reference to `vtable for resistor'|

Thank you for your answers below are the class resistor


public:
        resistor():circuitelement(){};
        resistor(double pimpedance, char pconnect):circuitelement(pconnect){complex<double>impedance(pimpedance,0);};
        ~resistor();
         void setfrequency();
         double getfrequency();
         complex<double> getimpedance();
         double magnitudeimpedance();
         double getphase();



Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is online

  • member icon


Reputation: 4141
  • View blog
  • Posts: 12,892
  • Joined: 25-December 09

Re: Populating char array in for loop and class problem

Posted 25 April 2012 - 10:56 AM

You have not posted enough code. Your first error is on line 191 in main.cpp. Where/what is contained on line 191. For the second error you need to show your complete class definition.

Jim
Was This Post Helpful? 0
  • +
  • -

#5 fml.max  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 10-December 09

Re: Populating char array in for loop and class problem

Posted 25 April 2012 - 12:15 PM

Sorry I was not explicit enough. The thing is part of a switch statemant which has two options. One to add in Series and One to add in Parallel. The Parallel case gives me the problem.

                char choice_2[numberofelements];
                double pimpedance[numberofelements];
                for(int i=0; i<numberofelements; i++)
                {
                    choice_2[i]=addelementsmenu(); //Problem occurs here. User input retrieved by function 
                    pimpedance[i]=elementinput(choice_2);
                    a.addelementparalell(choice_2,pimpedance,numberofelements);
                }

            }



Basically the addelementmenu function returns a char letter which identifies the type of circuitelement. This then ask the user for relevant info about the element in elementinput which returns a double.

I want to then pass on the char array and double array with number of elements to addelementparallel. The declaration is

void addelementparalell(char type[],double pimpedance[],int numberofelements);



I can paste the entire code if neccessary but it is work in progress and a bit convoluted atm. Hope this explains the problem better. Thanks for any help.
Was This Post Helpful? 0
  • +
  • -

#6 jimblumberg  Icon User is online

  • member icon


Reputation: 4141
  • View blog
  • Posts: 12,892
  • Joined: 25-December 09

Re: Populating char array in for loop and class problem

Posted 25 April 2012 - 12:21 PM

        choice_2[i]=addelementsmenu(); //Problem occurs here. User input retrieved by function 


How is addelemetsmenu() defined? How is it implemented? Does your function return type match the variable you are trying to put it into? The variable choice_2[i] is a single character, Is your function perhaps returning a char* and not a single character?

Without seeing the code that is causing the problems I am only guessing.

Jim
Was This Post Helpful? 0
  • +
  • -

#7 fml.max  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 10-December 09

Re: Populating char array in for loop and class problem

Posted 25 April 2012 - 12:23 PM

And here are the class definitions:

class circuitelement
{
    public:
        circuitelement(){complex<double> impedance(0.0,0.0); connectiontype='s';frequency=0;};
        circuitelement(char pconnect){connectiontype=pconnect;};
        virtual ~circuitelement();
        virtual void setfrequency()=0;
        virtual double getfrequency()=0;
        virtual complex<double> getimpedance()=0;
        virtual double magnitudeimpedance()=0;
        virtual double getphase()=0;
    protected:
    double frequency;// angular frequency
    complex<double> impedance ; // Impedance
    char connectiontype;//Either p or s

friend class circuit;
};



class resistor: public circuitelement
{
    public:
        resistor():circuitelement(){};
        resistor(double pimpedance, char pconnect):circuitelement(pconnect){complex<double>impedance(pimpedance,0);};
        ~resistor();
         void setfrequency();
         double getfrequency();
         complex<double> getimpedance();
         double magnitudeimpedance();
         double getphase();


};



Thanks for any hints.
Was This Post Helpful? 0
  • +
  • -

#8 fml.max  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 10-December 09

Re: Populating char array in for loop and class problem

Posted 25 April 2012 - 12:32 PM

Cheers Jim,

The function addelement is
char addelementsmenu()
{
//User Input
char input;
return input;
}



the function elementinput
double elementinput(char type)
{

double connect;

//User Input

return connect;

}




Thank you
Was This Post Helpful? 0
  • +
  • -

#9 jimblumberg  Icon User is online

  • member icon


Reputation: 4141
  • View blog
  • Posts: 12,892
  • Joined: 25-December 09

Re: Populating char array in for loop and class problem

Posted 25 April 2012 - 12:44 PM

Is that your actuall addelementsmenu() function? If it is the only thing I see wrong is that you never initialize your return value. If not post the actual function. Trying to see what is going wrong with only partial information is not working. Show the smallest possible complete program that illustrates your problem.


Jim
Was This Post Helpful? 1
  • +
  • -

#10 fml.max  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 10-December 09

Re: Populating char array in for loop and class problem

Posted 25 April 2012 - 01:14 PM

Thank You Jim!

The problem was a typo at the user input which would have initialised the char value. Looked over it a thousand times and did not spot it until now.

I still am running against a wall with


circuitelement.h|58|undefined reference to `vtable for resistor'|

The class declaration is posted above. Both derived and base class. I havn't overridden any of the functions just made declarations.

Thank you for your help!

Max
Was This Post Helpful? 0
  • +
  • -

#11 jimblumberg  Icon User is online

  • member icon


Reputation: 4141
  • View blog
  • Posts: 12,892
  • Joined: 25-December 09

Re: Populating char array in for loop and class problem

Posted 25 April 2012 - 01:18 PM

Implement the functions. That may solve the problem. Also you may want to read this link: The virtual table.

Jim
Was This Post Helpful? 0
  • +
  • -

#12 fml.max  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 10-December 09

Re: Populating char array in for loop and class problem

Posted 25 April 2012 - 04:26 PM

@Jim, sound advice. Basically there is a quirk g++ that an object is treated as virtual as long as not all virtual functions are overwritten. Therefore it can not be created which I tried to do.


Btw how do I mark the topic as solved?
Was This Post Helpful? 0
  • +
  • -

#13 jimblumberg  Icon User is online

  • member icon


Reputation: 4141
  • View blog
  • Posts: 12,892
  • Joined: 25-December 09

Re: Populating char array in for loop and class problem

Posted 25 April 2012 - 04:41 PM

Quote

Basically there is a quirk g++ that an object is treated as virtual as long as not all virtual functions are overwritten.

That is not a quirk in g++, that is a "feature" of the language.

Jim
Was This Post Helpful? 0
  • +
  • -

#14 fml.max  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 10-December 09

Re: Populating char array in for loop and class problem

Posted 26 April 2012 - 03:37 AM

I get that overridding the functions is neccessary when dealing with derived classes but the error comes up as a linker error as I declared all derived functions inline.

At least one must be out of line. See here http://gcc.gnu.org/faq.html#vtables. Defining one the constructors out of line allows the code to be compiled.

Thanks for your help Jim!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1