Thread: vectors of structs

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    30

    vectors of structs

    Hey I'm pretty new to C++ but have quite a bit of experience in Java. I have a project to make a Morse code translator. I have a struct that holds a letter and its morse code equivalent. Then I have a vector of these structs to hold all of the combinations. Here's the code thats giving me problems.

    Code:
    struct Code{
    		string letter;
    		string morse; 
    	};
    	vector<Code>* myConvertorTable;
    Code:
    void MorseCodeTranslator::setupConvertorTable()
    {
    	ifstream ifstr(myConversionTableFile.c_str());
    	if(!ifstr)
    	{
    		cerr << "Cannot open: " << myConversionTableFile << "." << endl;
    		exit(ERROR);	    
    	}
    
      string tempLetter;
      string tempMorse;
    
      while(getline(ifstr, tempLetter))
      {
    	myConvertorTable->letter.push_back(tempLetter);
    	if(getline(ifstr, tempMorse))
    	{
    		myConvertorTable->morse.push_back(tempMorse);
    	}
      }
    
    #ifdef DEBUG
      for(int i = 0; i < myConvertorTable->size(); i++)
      {
    	  cout << "morse: " << myConvertorTable->morse->at(i) << endl;
    	  cout << "letter: " << myConvertorTable->letter->at(i) << endl;
      }
    #endif
    
    }

    Heres the error messages.

    MorseCodeTranslator.cpp: In member function âvoid MorseCodeTranslator::setupConvertorTable()â:
    MorseCodeTranslator.cpp:19: error: âclass std::vector<MorseCodeTranslator::Code, std::allocator<MorseCodeTranslator::Code> >â has no member named âletterâ
    MorseCodeTranslator.cpp:22: error: âclass std::vector<MorseCodeTranslator::Code, std::allocator<MorseCodeTranslator::Code> >â has no member named âmorseâ

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    The error messages mean exactly what they say: vector<Code> does not, in fact, contain any letters, or any morses. It contains Codes. You need to build a Code and push_back that. You can use a tempCode, just like you're using a templetter and a tempmorse.

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> myConvertorTable->morse->at(i)

    You've got it backwards. Should be:

    myConvertorTable->at(i).morse
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  4. #4
    Registered User
    Join Date
    May 2008
    Posts
    30
    Thanks the temp struct idea worked.

  5. #5
    Registered User
    Join Date
    May 2008
    Posts
    30
    Ignore ME sorry I figured it out.
    Last edited by Zosden; 10-04-2008 at 06:45 PM. Reason: got it to work

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    And don't forget to allocate memory for your vector (and free it), since it's a pointer!
    But does it really [i]need[I] to be a pointer?
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How properly get data out of vectors of templates?
    By 6tr6tr in forum C++ Programming
    Replies: 4
    Last Post: 04-15-2008, 10:35 AM
  2. help using vectors with structs
    By Swordsalot in forum C++ Programming
    Replies: 15
    Last Post: 04-09-2008, 11:14 AM
  3. allocating structs within STL vectors
    By aoiOnline in forum C++ Programming
    Replies: 20
    Last Post: 12-05-2007, 02:49 PM
  4. Variable scopes; Vectors of struct's
    By relyt_123 in forum C++ Programming
    Replies: 8
    Last Post: 11-04-2007, 10:07 PM
  5. Vectors of structs
    By twomers in forum C++ Programming
    Replies: 7
    Last Post: 06-15-2006, 12:52 AM