Aug 13, 2016 at 1:16am Aug 13, 2016 at 1:16am UTC
I need help, because this is giving me an undefined reference to my class.
DNA.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#ifndef DNA_H
#define DNA_H
#include <string>
using namespace std;
class DNA{
private :
string dnaString;
public :
DNA(string dna);
DNA(); // default constructor
string changeString(string dna);
};
#endif
DNA.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
#include <iostream>
#include "DNA.H"
#include <string>
using namespace std;
DNA::DNA(string dna)
{
dnaString = dna;
}
DNA::DNA()
{
dnaString = " " ;
}
string DNA::changeString(string dna)
{
string newString;
for (int i = 0; i < dna.length(); i++)
{
if (dna[i] == 'G' )
dna[i] = 'C' ;
else if (dna[i] == 'T' )
dna[i] = 'A' ;
else if (dna[i] == 'C' )
dna[i] = 'G' ;
else if (dna[i] == 'A' )
dna[i] = 'T' ;
newString += dna[i];
}
return newString;
}
main.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include <iostream>
#include "DNA.H"
#include <string>
using namespace std;
int main()
{
DNA aDNA("GTATCCAATGCC" );
cout << aDNA.changeString("GTATCCAATGCC" );
system("pause" );
return 0;
}
Last edited on Aug 13, 2016 at 1:16am Aug 13, 2016 at 1:16am UTC
Aug 13, 2016 at 1:58am Aug 13, 2016 at 1:58am UTC
Your code runs OK as a single file so what is the full/complete error message and error reference number?
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
#include <iostream>
#include <string>
using namespace std;
class DNA{
private :
string dnaString;
public :
DNA(string dna);
DNA(); // default constructor
string changeString(string dna);
};
DNA::DNA(string dna)
{
dnaString = dna;
}
DNA::DNA()
{
dnaString = " " ;
}
string DNA::changeString(string dna)
{
string newString;
for (int i = 0; i < dna.length(); i++)
{
if (dna[i] == 'G' )
dna[i] = 'C' ;
else if (dna[i] == 'T' )
dna[i] = 'A' ;
else if (dna[i] == 'C' )
dna[i] = 'G' ;
else if (dna[i] == 'A' )
dna[i] = 'T' ;
newString += dna[i];
}
return newString;
}
int main()
{
DNA aDNA("GTATCCAATGCC" );
cout << aDNA.changeString("GTATCCAATGCC" );
return 0;
}
Last edited on Aug 13, 2016 at 2:00am Aug 13, 2016 at 2:00am UTC
Aug 13, 2016 at 2:49am Aug 13, 2016 at 2:49am UTC
I figured the problem is "DNA.H" and switch it to "DNA.h". There's also a problem with undefined reference to WinMain, which doesn't allow me to create this project with classes.
Aug 13, 2016 at 2:57am Aug 13, 2016 at 2:57am UTC
Using Visual Studio 2015 your header/source files setup works. Maybe you should rebuild? Sometimes errors can creep in when making changes and object files aren't updated.
Aug 13, 2016 at 6:52pm Aug 13, 2016 at 6:52pm UTC
personxd1,
Just a thought here if you are using Visual Studio as I do you need to include the #include "stdax.h"
file in each .cpp file. Once I put that include in the "DNA.cpp" file it compiled correctly. Just a thought and the program appeared to run OK.
Andy