0

Im trying to make a object / type that consists of an element of the periodic table. But when i try to use a vector of that object as a parameter, i get this error message expected a type, got ‘Element’

here is my code so far:

#include <iostream>
#include <string>
#include <vector>

using namespace std;

Element(int AtomicNumm, string Symboll, string Namee, double Weightt, 
int Neutronss, int Protonss, string ElectronConfigg) {
    string Name = Namee;
    int AtomicNum = AtomicNumm;
    string Symbol = Symboll;
    double Weight = Weightt;
    int Neutrons = Neutronss;
    int Protons = Protonss;
    string ElectronConfig = ElectronConfigg;
}
string returnElement(vector<Element> vec, string input) { // error here
    if (input.size() == 2) {
        for (int i = 0; i < vec.length(); i++) {

        }
    }
    return "";
}

int main(int argc, char*argv[]) {
    vector<Element> PT;
    string userinput (argv[1]);

    return -1;
}

Also, im new to c++. If objects work completely differently here please let me know. (Coming from java)

  • 2
    This isn't C++ syntax. Maybe you should read through a [tutorial](http://www.cplusplus.com/doc/tutorial/classes/) first? – bobshowrocks Nov 18 '18 at 02:23
  • @bobshowrocks So should i turn Element into a class? –  Nov 18 '18 at 02:24
  • 1
    Probably, yes. The block of code in your question called Element isn't valid C++. The link I provided should show you how to properly declare a class. Hold onto your butts, going from Java to C++ can be a bumpy ride. – bobshowrocks Nov 18 '18 at 02:31

1 Answers1

2

That's because you haven't declared 'Element' in your program. Syntactically, it is close to definition of a constructor.

To make your program work, i guess you can do following modification to existing element:

class Element {
    // your definition of element Here:

    // also include default constructor without any implementation 
    Element() {}
};
Abhinav
  • 1,496
  • 3
  • 15
  • 31
  • Would a default constructor be necessary, im just adding elements of the table myself. –  Nov 18 '18 at 02:32
  • A default constructor is always required. If you don't have any constructors defined, the compiler generates one for the class on the fly. But if you add any parametrized constructor, you have to add default one too! I believe if you compile without explicitly adding default constructor, you might get an error. – Abhinav Nov 18 '18 at 02:36
  • @Abhinav no, you dont. if you compile class myclass{}; the compiler happily generates the default member functions for you. – johnathan Nov 18 '18 at 06:37
  • @johnathan, please read and check facts carefully! People might get confused. If you aren't sure, please cross verify. For this, you can use https://stackoverflow.com/questions/4981241/no-default-constructor-exists-for-class – Abhinav Nov 18 '18 at 20:11
  • @Abhinav to be absolutely clear i was refering to the portion of your statement that reads "I believe if you compile without adding a default constructor, you might get an error". my response was very clear and https://ideone.com/38sJWv your welcome to view proof. the compiler generates default constructors when you do not supply a constructor. if you supply a constructor of any signature no defalt constructors will be generated by the compiler. – johnathan Nov 18 '18 at 20:26