C++: Unresolved Externals in MP3 assignment - c++

I'm working on a MP3 player for an assignment. I keep getting the following errors:
1>A4_main.obj : error LNK2019: unresolved external symbol "public: static void __cdecl Song::ClearListFile(void)" (?ClearListFile#Song##SAXXZ) referenced in function "void __cdecl DeleteSong(void)" (?DeleteSong##YAXXZ)
1>A4_main.obj : error LNK2001: unresolved external symbol "public: static char * Song::songListFile_" (?songListFile_#Song##2PADA)
1>A4_Song.obj : error LNK2001: unresolved external symbol "public: static char * Song::songListFile_" (?songListFile_#Song##2PADA)
1>A4_Song.obj : error LNK2019: unresolved external symbol __imp__mciSendStringA#16 referenced in function "public: void __thiscall Song::PlaySong(void)" (?PlaySong#Song##QAEXXZ)
1>A4_Song.obj : error LNK2001: unresolved external symbol "public: static char * Song::currentMP3_" (?currentMP3_#Song##2PADA)
From what I understand, these sort of errors stem from not including function declarations, declaring but not implementing them, misspelling, etc. What have I missed here? Since this is an assignment, I'll post the bare minimum of the code I think is causing the problem.
A4_main.cpp
#include "A4_LinkedList.h"
#include "A4_Song.h"
#include <iostream>
#include <fstream>
#include <cstdio>
#include <string>
using namespace std;
LinkedList g_list;
void FreeLinkedList();
char DisplayMenu();
void LoadSongFile();
void AddNewSong();
void DeleteSong();
void PlaySong();
void PrintAllSongs();
//stuff
void LoadSongFile()
{
const int SZ = 256;
int songCnt = 0;
ifstream inData;
char buff[SZ];
Song* newSong;
_flushall();
cout << "\nEnter the full file path: ";
cin.getline(Song::songListFile_, SZ);
// Open the file
inData.open(Song::songListFile_);
// Free any memory currently allocated for the word array
FreeLinkedList();
// Loop through file again and allocate memory
while (!inData.eof())
{
// Each time through loop read all 5 entries in each line.
// Songt with the Song name
inData.getline(buff, SZ);
if (buff[0] == 0)
{
// No more words
break;
}
// Create a new Song object
newSong = new Song(buff);
if (newSong == 0)
{
cout << "\nDynamic memory allocation failed.";
break;
}
// Add this Song object to the linked list
g_list.AddLinkToBack(newSong);
songCnt++;
}
inData.close();
cout << "\nLoaded file and read " << songCnt << " Song objects.";
}
void DeleteSong()
{
const int SZ = 256;
bool foundSong = false;
Node* node = g_list.GetFirstNode();
Song* song = 0;
char songFileName[SZ];
_flushall();
// Prompt the user for the name of a song
cout << "\nEnter the song name with extension: ";
cin.getline(songFileName, SZ);
// Loop through the linked list for that song and delete it if it is found.
// If not, print error to console.
while (node != 0)
{
// Cast the void ptr to a song object ptr
song = (Song*)(node->data_);
// Call on the Song class to print the objects contents
if (strcmp(song->GetSongName(), songFileName) == 0)
{
// Set flag and get out of loop
g_list.RemoveThisLink(node);
foundSong = true;
break;
}
// Go to the next node
node = node->next_;
}
if (!foundSong)
{
cout << "\nCould not find that song in list!\n";
}
else
{
// Now that the linked list has been updated need to persist the new
// list to the song file, replacing previous contents.
Song::ClearListFile();
// Now loop through the linked list again, appending the song
// file name to the song list file.
node = g_list.GetFirstNode();
while (node != 0)
{
// Cast the void ptr to a song object ptr then add name to file
song = (Song*)(node->data_);
song->AppendToListFile();
// Go to the next node
node = node->next_;
}
}
}
A4_Song.cpp
#include "A4_Song.h"
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <Windows.h>
using namespace std;
// Store the path name of the song list file
static char songListFile_[ENTRY_SZ] = "";
static char currentMP3_[ENTRY_SZ] = "";
// Static method to empty the song list file
static void ClearListFile()
{
ofstream outFile;
if (songListFile_[0] != 0)
{
// Open for truncate then close again
outFile.open(songListFile_, ios_base::ate);
outFile.close();
}
else
cout << "\nNothing to clear!";
}
void Song::PlaySong()
{
const int BUFF_SZ = 512;
char fullStr[BUFF_SZ];
MCIERROR err;
StopSong();
// Set global variable so we know this file is playing.
// Sandwich the file name in escaped double quotes so
// spaces can be included and don't need to double up
// on the backslashes.
sprintf_s(currentMP3_, ENTRY_SZ, "\"%s\"", songPath_);
sprintf_s(fullStr, BUFF_SZ, "open %s type mpegvideo alias myFile", currentMP3_);
err = mciSendString(fullStr, NULL, 0, 0);
err = mciSendString("play myFile", NULL, 0, 0);
}
Let me know if I've omitted too much.

Your problem is that you have variables and functions declared inside Song, but are defined at namespace level. This makes them different entities, and so definitions for the declarations in Song are never found.
static char songListFile_[ENTRY_SZ] = "";
static char currentMP3_[ENTRY_SZ] = "";
static void ClearListFile() {/*...*/}
These should be changed. Removing static and prefixing the surrounding class Song should fix it.
char Song::songListFile_[ENTRY_SZ] = "";
char Song::currentMP3_[ENTRY_SZ] = "";
void Song::ClearListFile() {/*...*/}
You only need the static inside the class definition. Outside it, you're using a different meaning of the static keyword. I think you have more instances like this outside the code you posted, but it shouldn't be difficult to find them.

Related

LNK2019 error with Linked Lists ( c programming)

I'm new at c programming, and during my learnings, lately I've started to deal with Linked lists. In this program that I wrote, i keep getting this message( LNK2019 error):
unresolved external symbol _main referenced in function "int __cdecl invoke_main(void)" (?invoke_main##YAHXZ)
What I'm trying to do is to create a linked list, and use a function to input values into this list, using the main.
this is the full code I wrote:
#include <stdio.h>
#include <stdlib.h>
typedef struct Original_list
{
int data;
struct Original_list *next;
}original_list;
original_list *Input();
void EX2()
{
original_list *list;
list = Input();
}
original_list *Input()
{
original_list *lst, *curr_point;
int c;
printf("Please enter a value to the first data: \n");
scanf_s("%d", &c);
if (c < 0)
return NULL;
lst = (original_list*)malloc(sizeof(original_list));
curr_point = lst;
lst->data = c;
while (c >= 0)
{
curr_point->next = (original_list*)malloc(sizeof(original_list));
curr_point = curr_point->next;
curr_point->data = c;
printf("please enter number(scan will stop if a negative number is scanned): \n");
scanf_s("%d", &c);
}
curr_point->next = NULL;
return lst;
}
I can't see any definition I did wrong or any problem justifying this error.
please help!
thank you very much!
Your code lacks an entry point. For C/C++ it is usually main(), that's what the error is about.
Check the projects configuration and make sure that you have set
Linker > System > Subsystem
to 'Console'. The issue occurs when it is set to 'Windows'.

error LNK2019: unresolved external symbol, cannot not link with the cpp file i coded [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 7 years ago.
I start learning C++ in school and this error appear.
1>Bettle_Dice.obj : error LNK2019: unresolved external symbol "public: int __thiscall Beetle::checkcom(void)" (?checkcom#Beetle##QAEHXZ) referenced in function _main
I have include other header files and cpp files, I don understand why only this file have problem please help
Below is my code
main.cpp
#include "stdafx.h"
#include "beetle.h"
#include "dice.h"
#include "player.h"
#include <iostream>
using namespace std;
int main()
{
Player p; //declare Class to a variable
Dice d;
Beetle btle;
int temp;
cout << "Number of players?" << endl;
cin >> temp;
p.Num(temp); //store the number of player into class
//cout << p.getNumPlayers() <<endl;
cout << "Start game!!" <<endl;
temp = btle.checkcom();
while(temp != 1)
{
for(int i=0;i<p.getNumPlayers();i++)
{
temp = d.roll();
cout <<"Your roll number:"<< temp;
}
}
return 0;
}
beetle.h
class Beetle
{
private:
int body,head,ante,leg,eye,tail;
public:
int completion();
int checkcom();
int getBody() { return body;};
int getHead() { return head;};
int getAnte() { return ante;};
int getLeg() { return leg;};
int getEye() { return eye;};
int getTail() { return tail;};
};
Beetle.cpp
#include "stdafx.h"
#include "beetle.h"
int completion()
{
return 0;
}
int checkcom()
{
Beetle btle;
int flag = 0;
if(btle.getBody() == 1 && btle.getHead() == 1 && btle.getAnte() == 2 && btle.getEye() == 2 && btle.getLeg() ==6 && btle.getTail() == 1)
flag = 1;
return flag;
}
I checked some solution on the internet, some are saying is the library problem, but this file is not a built-in function. I guess it is not the problem of the library. I tried to include the beetle.obj file to it and the debugger said it is included already and duplicate definition.
In the other file, i do not have "bettle" this word. It should not be the problem of double declaration or duplicate class.
I have no idea what the problem is. Please help.
You need to prefix the signature of your class functions with the class name Beetle::
Otherwise the compiler just thinks those functions are global functions, not member functions.

Error Code LNK2019 & LNK1120 [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 7 years ago.
I create this file over and over and cant seem to see why I'm getting this error. I tried going to the line where the code is but the format seem correct I may just need another set of eyes .
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
void readString(char*, int);
void changeToUppercase(char*, int);
void displayStringInUppercase(char*, int);
int main()
{
int arraySize;
char* characterArray;
cout << "Enter the size of dynamic array: ";
cin >> arraySize;
characterArray = new char[arraySize];
readString(characterArray, arraySize);
changeToUppercase(characterArray, arraySize);
displayStringInUppercase(characterArray, arraySize);
delete [] characterArray;
system ("pause");
return 0;
}
void changeToUppercase(char* characterArray, int arraySize)
{
for(int i = 0; i < arraySize; i++)
characterArray[i] = toupper(characterArray[i]);
}
void displayStringInUppercase(char* characterArray, int arraySize)
{
cout << "\nThestring inupper case letters: ";
for(int i = 0; i < arraySize; i++)
characterArray[i] = toupper(characterArray[i]);
}
This is the error codes that keep popping up:
error LNK2019: unresolved external symbol "void __cdecl readString(char *,int)" (?readString##YAXPADH#Z) referenced in function _main
fatal error LNK1120: 1 unresolved externals
You use a forward declaration: void readString(char*, int); but then never actually define this function.
Define your readString function later in your code like...
void readString(char* str, int a)
{
// do stuff
}
You are missing the readString function. You have a forward declaration that satisfies the compiler here
void readString(char*, int);
But no actual implementation of the function to satisfy the linker when it tries to put your program together. You need something along the lines of
void readString(char* characterArray, int arraySize)
{
// do stuff here
}

Link error 2019, prototypes are in header file though

I'm new to c++ and was just trying to do a question that involves 3 files.
I got a header file with a namespace that declares a structure and some prototypes. In definitions.cpp file, I have all the function definitions. Then the main.cpp file, just creates a few structures, calls some functions to fill the structures, then calls another function to display the structures.
The problem I have is it gives me an unresolved external error. From what I can gather I think the compiler is complaining that it cant find the prototypes for the functions being called in main.cpp - the overloaded setStruct() and the showStruct().
But I thought when I included the header.h file and declared the functions with the using declaration, in main.cpp, it gave the compiler access to the prototypes stored in the header file?
Header.h
#ifndef header_h
#define header_h
namespace SALES
{
const int QUARTERS = 4;
struct Sales
{
double sales[QUARTERS];
double average;
double max;
double min;
};
void setSales(Sales & s, const double ar[], int n);
void setSales(Sales & s);
void showSales(const Sales & s);
}
#endif
Definitions.cpp
#include<iostream>
#include"header.h"
using SALES::Sales;
using SALES::QUARTERS;
double max(Sales & s) //find max sale value in sales array
{
double maxVal = s.sales[0];
for(int i = 1; i<4;i++)
{
if(s.sales[i]>maxVal)
{
maxVal = s.sales[i];
}
}
return maxVal;
}
double min(Sales & s) //find min sale value in sales array
{
double minVal = s.sales[0];
for(int i = 1; i<4;i++)
{
if(s.sales[i]<minVal)
{
minVal = s.sales[i];
}
}
return minVal;
}
void setSales(Sales & s) // fill sales structure interactivly
{
std::cout<< "Please enter the sales for the yearly quarters.\n";
for(int i = 0;i<QUARTERS;i++)
{
std::cout<< "Quater "<<i+1<<": ";
while(!(std::cin>>s.sales[i]))
{
std::cout<<"Please enter valid input\n";
std::cout<< "Quater "<<i+1<<": ";
std::cin.clear();
std::cin.ignore();
}
}
s.average = ((s.sales[0]+s.sales[1]+s.sales[2]+s.sales[3])/4);
s.max = max(s);
s.min = min(s);
}
void setSales(Sales & s, const double ar[], int n) // fill sales structure non interactivly
{
for(int i = 0;i<n;i++)
{
s.sales[i] = ar[i];
}
for(int i = n;i<QUARTERS;i++)
{
s.sales[i] = 0;
}
s.average = ((s.sales[0]+s.sales[1]+s.sales[2]+s.sales[3])/4);
s.max = max(s);
s.min = min(s);
}
void showSales(const Sales & s) // display structure
{
std::cout<< "\nSales for the year\n";
for(int i = 0;i<QUARTERS;i++)
{
std::cout<<"Quarter "<<i+1<<": $"<<s.sales[i];
}
std::cout<<"Max Sale: "<<s.max<<std::endl;
std::cout<<"Min Sale: "<<s.min<<std::endl;
std::cout<<"Average of sales: "<<s.average<<std::endl;
}
Main.cpp
#include<iostream>
#include"header.h"
using SALES::Sales;
using SALES::setSales;
using SALES::showSales;
int main()
{
double Sales1[4] = {453.50, 654.60, 340.20, 500.30};
Sales Year1;
Sales Year2;
setSales(Year1, Sales1, 3);
setSales(Year2);
showSales(Year1);
showSales(Year2);
return 0;
}
Error
1>------ Build started: Project: Myfirst, Configuration: Debug Win32 ------
1> Main.cpp
1>Main.obj : error LNK2019: unresolved external symbol "void __cdecl SALES::setSales(struct SALES::Sales &,double const * const,int)" (?setSales#SALES##YAXAAUSales#1#QBNH#Z) referenced in function _main
1>Main.obj : error LNK2019: unresolved external symbol "void __cdecl SALES::setSales(struct SALES::Sales &)" (?setSales#SALES##YAXAAUSales#1##Z) referenced in function _main
1>Main.obj : error LNK2019: unresolved external symbol "void __cdecl SALES::showSales(struct SALES::Sales const &)" (?showSales#SALES##YAXABUSales#1##Z) referenced in function _main
1>E:\Documents\Uni\Programming\C++ starter projects\Myfirst\Debug\Myfirst.exe : fatal error LNK1120: 3 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Any help would be great, thanks.
When you define the setSales etc functions, you need to tell the compiler that they are in the SALES namespace. Putting
namespace SALES
{
// functions here
}
will do it.
You are currently defining your functions outside of any namespace, and that is the reason for the linker not finding the definitions within the namespace where those functions are declared and recognized.
The fix would be to wrap the functions in your implementation file - Definitions.cpp -, with the namespace similarly how you are doing it in the header file.
This is what I would write if I were you:
namespace SALES
{
...
void setSales(Sales & s) // fill sales structure interactivly
...
std::cout<<"Average of sales: "<<s.average<<std::endl;
}
}
If you do not put the rest (your min and max) into the namespace, use the :: scope specifier just in case, although I would suggest to put everything into your own namespace.

Visual C++ Linker Error 2019

I'm trying to include a simple hash table class in some files with a header class. But whenever I try to compile I get several errors like this:
LNK2019: unresolved external symbol " public: __thiscall HashTable::~HashTable(void)" (??1HashTable##QAE#XZ) referenced in function _main "
I'm using Visual Studio 2010. I am aware that this means it can't find the function definition in any of the source files. But I have defined them, in a file in the same directory as the file it's called in. Perhaps Visual Studio doesn't look in the current directory unless you set some linker option?
Here is the source code:
//HashTable.h
#ifndef HASH_H
#define HASH_H
class HashTable {
public:
HashTable();
~HashTable();
void AddPair(char* address, int value);
//Self explanatory
int GetValue(char* address);
//Also self-explanatory. If the value doesn't exist it throws "No such address"
};
#endif
//HashTable.cpp
class HashTable {
protected:
int HighValue;
char** AddressTable;
int* Table;
public:
HashTable(){
HighValue = 0;
}
~HashTable(){
delete AddressTable;
delete Table;
}
void AddPair(char* address, int value){
AddressTable[HighValue] = address;
Table[HighValue] = value;
HighValue += 1;
}
int GetValue(char* address){
for (int i = 0; i<HighValue; i++){
if (AddressTable[HighValue] == address) {
return Table[HighValue];
}
}
//If the value doesn't exist throw an exception to the calling program
throw 1;
};
};
No you have not. You created a new class.
The proper way to define the methods is:
//HashTable.cpp
#include "HashTable.h"
HashTable::HashTable(){
HighValue = 0;
}
HashTable::~HashTable(){
delete AddressTable;
delete Table;
}
void HashTable::AddPair(char* address, int value){
AddressTable[HighValue] = address;
Table[HighValue] = value;
HighValue += 1;
}
int HashTable::GetValue(char* address){
for (int i = 0; i<HighValue; i++){
if (AddressTable[HighValue] == address) {
return Table[HighValue];
}
}
//If the value doesn't exist throw an exception to the calling program
throw 1;
};