Issue with headers (C4603, C1020)

Hiya!
I've been getting some different errors
I get a
1."warning C4603: 'read_words': macro is not defined or definition is different after precompiled header is use"
and a
2."error C1020:unexpected #endif"


read_words.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
#pragma once
#ifndef read_words
#define read_words
#include "stdafx.h"
#include <iostream>
#include <vector>
#include <string>


void readwords(std::vector<std::string> words){
	std::string x;
	words.clear();
	try{
		while (std::cin >> x)
		{
			words.push_back(x);
		}
	}
	catch (std::domain_error)
	{
		std::cout << "There was an error, dawg.\n";
	}

}
#endif; 


project 3.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include "stdafx.h"
#include "read_words.cpp"

using namespace std;

int main()
{
	cout << "Input words -> ";
	vector<string> words;
	readwords(words);
	cout << words.at(0);

	return 0;
}


For the first error I am not sure what I am supposed to see in order to fix it and for the second error I am confused why its unexpected.

--------------------------------------------------------
Also, when I define the function readwords() in another item, these problems are fixed, but I get 3 errors: "LNK2005'...'","LNK2005:_main already defined in project 3.obj", and"LNK1169:one or more multiply defined symbols found. proect 3.exe".
Last edited on
I don't see the point of putting read_words in a separate file. Why not just declare/define it in the main file? Also while including .cpp files isn't disallowed it is not good practice. Can cause linker and symbol issues like the ones you are experiencing.
I'll need readwords function in a different file (practicing headers).
I get the same errors when I transfer read_words.cpp contents to read_words.h and change the
#include "read_words.cpp" to #include "read_words.h" in project 3.cpp.
You need to pass in the vector as a reference so it can populate in the function.

read_words.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
#pragma once

#ifndef READ_WORDS_H
#define READ_WORDS_H

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

void readwords(std::vector<std::string> &words) // pass in reference to vector
{
  std::string x;
  words.clear();
  std::cout << "Input words -> ";
  try
  {
    while (std::cin >> x) // had to hit Ctrl-Z to end input loop
    {
      words.push_back(x);
    }
  } catch (std::exception e) // catches possible exception but you don't bother displaying it
  {
    std::cout << "There was an error, dawg.\n";
  }
}

#endif 


project 3.cpp
1
2
3
4
5
6
7
8
9
10
#include "read_words.h"

int main()
{
  std::vector<std::string> words;
  readwords(words); // will pass in words to function for population because readwords takes reference
  std::cout << words.at(0);

  return 0;
}
Last edited on
Thank you Texan40,
That cleared 3 of the 5 errors that came up from my Visual Studio Express.
The two that are displaying now are
"error LNK2005: _main already defined in project 3.obj" in source.obj
and
" error LNK1169: one or more multiply defined symbols found" in project.exe
These seem to be an issue with the compiler itself. Not really sure how to approach it other than opening a new project and moving contents over.
Last edited on
Topic archived. No new replies allowed.