I've simplified it further and I still have the same problem!
split.h:
Code:
#ifndef String_split_Guard
#define String_split_Guard
#include <string>
#include <vector>
std::vector<std::string> split (const std::string& s);
#endif
split.cpp:
Code:
#include <vector>
#include <string>
#include <cctype>
#include "split.h"
using namespace std;
vector<string> split (const string& s) // function that splits a string and inserts the individual
{ // words into a vector<string>
vector<string> ret;
typedef string::size_type string_size;
string_size i = 0;
while (i != s.size())
{
while (i != s.size() && isspace(s[i])) // finds the first non-space char - will be at position i
i++;
string_size j = i;
while (j != s.size() && !isspace(s[j])) // finds the first space character after i - will be at position j
j++;
if (i != j) // if any words have been found
{
ret.push_back(s.substr(i, j-i)); // pushes back the word found between i and j
i = j;
}
}
return ret;
}
wintest.cpp (my main file):
Code:
// Wintest.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
using namespace std;
int main(int argc, char* argv[])
{
string input1;
vector<string> input2;
input2 = split (input1);
return 0;
}
stdafx.h: (that weird header thing that MSVC seems to need)
Code:
// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently, but
// are changed infrequently
//
#if !defined(AFX_STDAFX_H__7EF3AE03_69E3_11D6_82E3_005004F6DA69__INCLUDED_)
#define AFX_STDAFX_H__7EF3AE03_69E3_11D6_82E3_005004F6DA69__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// TODO: reference additional headers your program requires here
#include <string>
#include <vector>
#include "split.h"
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.
#endif // !defined(AFX_STDAFX_H__7EF3AE03_69E3_11D6_82E3_005004F6DA69__INCLUDED_)
If i don't use MSVC i don't have the stdafx.h header, and instead my includes are before the int main() function.
I get the error whether in codewarrior or msvc:
Wintest.obj : error LNK2001: unresolved external symbol "class std::vector<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::allocator<class std::basic_string<char,struct std::char_traits<char>,class
std::allocator<char> > > > __cdecl split(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)" (?split@@YA?AV?$vector@V?$basic_string@DU?$char_tr aits@D@std@@V?$allocator@D@2@@std@@V?$allocator@V? $basic_st
ring@DU?$char_traits@D@std@@V?$allocator@D@2@@std@ @@2@@std@@ABV?$basic_string@DU?$char_traits@D@std@ @V?$allocator@D@2@@2@@Z)
Debug/Wintest.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.
It works fine if i just copy the split.cpp code into wintest.cpp (making sure I have the relevant includes in stdafx.h) What's going on??