Undefined reference to default constructor?

Hello, I'm a bit new to c++ and trying to practice the object-oriented part of it by converting my java programs into c++. I believe I understand the concepts of a header file and declaring the functions in the .cpp files. I keep getting this "Undefined reference to NamedStorm::NamedStorm()" error. I researched and asked in many places and never got a clear answer. Can you guys help?


NamedStorm.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#ifndef NAMEDSTORM_H
#define NAMEDSTORM_H

#include <string>
#include <iostream>

// NEVER use using namespce in header, use std instead.
using std::string;


class NamedStorm{
private:
    std::string stormName;
    std::string stormCategory;
    double maxWindSpeed;
    double stormPressure;
    static int stormCount;

public:

    // Constructor
    NamedStorm(std::string, double, std::string, double);
    NamedStorm(std::string);
    NamedStorm();

    // Destructor
    //~NamedStorm();

    // Get functions
    int getStormCount();
    double getStormPressure();
    double getWindSpeed();
    std::string getStormCategory();
    std::string getName();

    // Set functions
    static void displayOutput();
    static void sortByNames();
    static void sortByWindSpeed();
    static void getAverageWindSpeed();
    static void getAverageStormPressure();
};

#endif // NAMEDSTORM_H_INCLUDED 


NamedStorm.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// CPP => Function definition
#include <string>

#include "NamedStorm.h"

using std::string;

// Defining static variables
int NamedStorm::stormCount = 0;

// Constructor definition
NamedStorm::NamedStorm(std::string sName, double wSpeed, std::string sCat, double sPress){
    stormName = sName;
    windSpeed = wSpeed;
    stormCategory = sCat;
    stormPressure = sPress;
    stormCount++;
}

NamedStorm::NamedStorm(std::string sName){
    stormName = sName;
    stormCount++;
}

NamedStorm::NamedStorm(){
    stormName = sName
    stormCount++;
}

// Destructor definition
//NamedStorm::~NamedStorm(){}

// Get (Accessor) function definition
int NamedStorm::getStormCount(){
    return stormCount;
}

double NamedStorm::getStormPressure(){
    return stormPressure;
}

string NamedStorm::getStormCategory(){
    return stormCategory;
}

string NamedStorm::getName(){
    return stormName;
}

// Set (Mutator) function definition (These are blank for the sake of keeping // things simple
void NamedStorm::displayOutput(){}
void NamedStorm::sortByNames(){}
void NamedStorm::getAverageStormPressure(){}
void NamedStorm::getAverageWindSpeed(){}
void NamedStorm::getWindSpeed(){}


Main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <string>

#include "NamedStorm.h"

using namespace std;

NamedStorm storm[2];

int main(){
   // NamedStorm Chris("Chris", 70.0, "T.S.", 990.0);
  // storm[0] = Chris;
    return 0;
}
It looks like that file NamedStorm.cpp was not included in the project. That is when the linker processed the object file with main it did not have access to the object file of NamedStorm.cpp
1
2
3
4
NamedStorm::NamedStorm(){
    stormName = sName
    stormCount++;
}

missing semicolon?
What can I do, I know nothing about what linkers are. I'm using Code::Blocks as my IDE by the way.

to abhishkm: I fixed that, still the issue is there
Last edited on
To cire: I did that too. Kinda stomped to the point of giving up until my c++ class starts. My professor can probably figure it out

Maybe there something wrong with the way the files are placed?
http://i.imgur.com/zhEtcyp.jpg

Also, is my syntax at least correct?
Last edited on
Very good. Just a couple errors.

NamedStorm.cpp
line 14: no variable named "windSpeed" (did you mean "maxWindSpeed"?)
line 21: missing semicolon
line 55: function header does not match declaration in header file

(I rewrote it to make it work:
55
56
double NamedStorm::getWindSpeed(){ return maxWindSpeed; }

And here's a modified main.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <string>

#include "NamedStorm.h"

using namespace std;

NamedStorm storm[2];

int main(){
    using namespace std;
    storm[0] = NamedStorm("Chris", 70.0, "T.S.", 990.0);
    cout << "Storms this season: " << storm[0].getStormCount() << "\n";
    cout << storm[0].getName() << " has a wind speed of " << storm[0].getWindSpeed() << ".\n";
    return 0;
}

Output is:

Storms this season: 3
Chris has a wind speed of 70.

You'll notice that the number of storms this season is wrong. You'll need to add that destructor in and decrement stormCount. (It happened because of the temporary created on line 12 of main().)

I don't know what's wrong with your C::B.

Hope this helps.
Im relieved that its not my fault, but the compiler. VS and C::B have been acting very strange lately. Thanks for the help, I'll just reinstalled them both
Topic archived. No new replies allowed.